( subshell )
to avoid having to cd
back SH-210348createdb botdb
49createuser botuser
50
51cd ..52echo "pg_ctl -D $PREFIX/var/lib/postgresql start" > startbot.sh
53echo "cd OpenUserBot" >> startbot.sh
54echo "python3 -m userbot" >> startbot.sh
When specifying a command such as cd dir; somestuff; cd ..
, cd dir
can fail when permissions are lacking, if dir
was deleted, or if dir
is actually a file.
In this case, somestuff
will run in the wrong directory and cd ..
will thus leave you in the parent directory, which is most certainly not intended.
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 cd
s exit status and/or use subshells to limit the effects of cd
when it fails.
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" || exit
convert index.png index.jpg
cd ..
done
If you set variables that you can't use in a subshell, you will probably need to use this method. In such a case, you should definitely check the exit status of cd
. This will also silence this suggestion.