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
2 Answers 2
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– Costas2015年04月25日 22:38:06 +00:00Commented Apr 25, 2015 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– Stéphane Chazelas2015年04月26日 08:43:01 +00:00Commented Apr 26, 2015 at 8:43 -
1@StéphaneChazelas -
find
's probably overkill - recursing a tree and executing unknown scripts - likely as root - is crazy. Iffind
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– mikeserv2015年04月26日 10:44:24 +00:00Commented Apr 26, 2015 at 10:44
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.