You need to run some commands, but you only want to run certain commands if certain other ones succeed. For example, you’d like to change directories (using the cd command) into a temporary directory and remove all the files. However, you don’t want to remove any files if the cd fails (e.g., if permissions don’t allow you into the directory, or if you spell the directory name wrong).
Solution
We can use the exit status ($?) of the cd command in combination with an ifstatement to do the rm only if the cd was successful.
cd mytmp if (( $? )); then rm * ; fi
Discussion
Obviously, you wouldn’t need to do this if you were typing the commands by hand. You would see any error messages from the cd command, and thus you wouldn’t type the rm command. But scripting is another matter, and this test is very well worth doing to make sure that you don’t accidentally erase all the files in the directory where you are running.
Let’s say you ran that script from the wrong directory, one that didn’t have a subdirectory named mytmp. When it runs, the cd would fail, so the current directory remains unchanged. Without theifcheck (the cd having failed) the script would just continue on to the next statement. Running therm *would remove all the files in your current directory. Ouch. Theifis worth it.
So how does$?get its value? It is the exit code of the command. For C Language programmers, you’ll recognize this as the value of the argument supplied to theexit()function; e.g.,exit(4);would return a 4. For the shell, zero is considered success and a non-zero value means failure.
If you’re writing bash scripts, you’ll want to be sure that your bash scripts explicitly set return values, so that$?is set properly from your script. If you don’t, the value set will be the value of the last command run, which you may not want as your result.
See Also
Recipe 4.2, "Telling If a Command Succeeded or Not"
Recipe 4.6, "Using Fewer if Statements"
Please check back next week for the conclusion to this article.