Exiting the Script
So far in our sample scripts, we terminated things pretty abruptly. When we were finished with our last command, we just ended the script. There's a more elegant way of completing things available to us.
Every command that runs in the shell uses an exit status
to indicate to the shell that it's finished processing. The exit status is an integer value between 0 and 255 that's passed by the command to the shell when the command finishes running. You can capture this value and use it in your scripts.
Checking the exit status
Linux provides the $?
special variable that holds the exit status value from the last command that executed. You must view or use the $?
variable immediately after the command you want to check. It changes values to the exit status of the last command executed by the shell:
$ date
Sat Jan 15 10:01:30 EDT 2014
$ echo $?
0
$
By convention, the exit status of a command that successfully completes is zero. If a command completes with an...