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
find /etc/postinstall -name '*.sh' -exec bash -c 'echo Runing {} ...; "{}" ; mv "{}" "{}.done"' \;
– Costas Apr 25 '15 at 22:38-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 08:43find
'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 Apr 26 '15 at 10:44