-
Notifications
You must be signed in to change notification settings - Fork 1.9k
SC2103
Vidar Holen edited this page Jan 21, 2015
·
6 revisions
for dir in */
do
cd "$dir"
convert index.png index.jpg
cd ..
done
for dir in */
do
(
cd "$dir" || exit
convert index.png index.jpg
)
done
or
for dir in */
do
cd "$dir" || continue
convert index.png index.jpg
cd ..
done
When doing cd dir; somestuff; cd .., cd dir can fail when permissions are lacking, if the dir was deleted, or if dir is actually a file.
In this case, somestuff will run in the wrong directory and cd .. will take you to an even more wrong directory. In a loop, this will likely cause the next cd to fail as well, propagating this error and running these commands far away from the intended directories.
Check cds exit status and/or use subshells to limit the effects of cd.
None known.