7 ms·
Parsing ls is an anti-pattern, but the author says it works for years - we all do mistakes and as long as it works you don't notice. And it's an easy fix:
by rkta 2y ago
Parsing ls is an anti-pattern, but the author says it works for years - we all do mistakes and as long as it works you don't notice.
And it's an easy fix:
- for project in $(ls go-cicd); do
+ cd go-cicd || exit 1; for project in \*; do
- hawski 2y agoI would suggest not to mess with the current working directory and instead do something like this: for project in go-cicd/*; do project="${project#*/}"
- bravetraveler 2y agoIndeed, thank you for mentioning that - I was going to suggest similar. That said, if you do (ie: scratch files, things outside of control, whatever), consider the directory stack: https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html https://www.gnu.org/software/bash/manual/html_node/Directory... Using 'pushd' and 'popd' can save your fingers/brain from getting lost in context.
- arp242 2y agoI like using subshells for this: ( cd dir for f in ./*; [..] ) There's a few scenarios where this won't work (mainly if you want to set a variable from the "outer scope"), but 99% of the time it works nicely.
- bravetraveler 2y agoAh, that's a nice/neat thought - thank you for sharing! Makes total sense, temporary/disposable shell for such things