3

This question already has an answer here:

The following link recommends against using loops in shells.

bash variables in for loop range

Why is this? Here is an example loop I just happened to be looking at when I came across that answer:

find /etc/postinstall -name '*.sh' | while read script
do
  echo Running $script ...
  $script
  mv $script $script.done
done

marked as duplicate by Celada, Stéphane Chazelas bash Apr 26 '15 at 8:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Two application types come to my mind where shell loops are not considered to be the best approach. The first is data processing; many tools (like sed, awk, perl, etc.) do the loop implicitly and much more performant. The second is (like in your sample code), where some code is executed for a set of files, where find with the -exec switch can also already execute commands (also a shell) with less problems and overhead. There are probably more cases, but those two mentioned should already be enlightening. That said; shell loops are not inherently bad, or somesuch. Just take other options (like the two mentioned) into your consideration.

  • @StevenPenny find /etc/postinstall -name '*.sh' -exec bash -c 'echo Runing {} ...; "{}" ; mv "{}" "{}.done"' \; – Costas Apr 25 '15 at 22:38
  • 1
    @Costas, that would run one bash per file and embed {} in the code is a lot worse practise than using loops. I'd still use a loop like -exec sh -c 'for script do...' sh {} + here or (GNUly): find ... -printf 'Executing %f\n' -exec {} \; -exec mv {} {}.done \; (which would run the mv only if the script succeeds). – Stéphane Chazelas Apr 26 '15 at 8:43
  • @StéphaneChazelas - find's probably overkill - recursing a tree and executing unknown scripts - likely as root - is crazy. If find called a script which altered the mount tree it could go really bad. And renaming files in /etc should be avoided. As I see it the op should handle only files named to some expected conformity - like the common 1-99 prefixes - and just dot each in the order theyre globbed in a for loop - each in its own subshell if need be. I believe that's something like what he intends to do now anyway. – mikeserv Apr 26 '15 at 10:44
0

Shell is a terrible programing language, but it is very handy. I write a lot of shell scripts, and on occasion I use loops. I have a script that takes a couple hours to execute. It needs rewritten in something else. If you have a loop in your shell script it is an indicator that it might be time to start thinking about rewriting that script before it becomes completely unmanageable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.