#!/bin/bashcheckParms
if [ $? != 0 ]; then
echo Exiting: checkParms failed
exit 1
fi
Remember that the exit code when a script ends will depend on the final command that’s run. The script beneath would finish with an exit code of 0 if the exit command weren’t included because the if/then command doesn’t generate any errors.
#!/bin/bashcontact /tmp/log 2> /dev/null
if [ $? -eq 0 ]
then
echo "File efficiently created"
else
echo "Couldn't create file"
exit 1
fi
Exit code numeric vary
The vary of values for exit codes runs from 0 to 255. If you happen to concern an “exit 256” command in a script after which examine the exit standing, you’ll discover that it’s equal to 0! For the reason that exit code is just going to occupy a single byte, you possibly can consider it as a modulo 256. “Exit 258”, equally, will go away you with an exit code of two.
Different particular exit codes embrace 2 (meant to point the misuse of a shell builtin), 127 (command not discovered), 128 (invalid exit code), an entire bunch of exit codes above 128 (128 + a sign for deadly errors) and 130 (management C deadly error). Exit code 1, alternatively, is a catchall for varied errors. If you wish to outline exit codes to point varied issues in your individual scripts, you’re in all probability higher off utilizing codes between 3 and 125 or, as has been recommended by some contributors to the furtherance of fine return code utilization, 64 to 113. In both case, you’ve got a variety of attainable return codes at your disposal. Standardizing on a set that fits your wants would possibly streamline your coding a bit.
Right here’s an inventory of exit codes and the kinds of errors they characterize:
- 0: Success. The command or script executed with none error.
- 1: Basic error. The command or script failed for some unspecified cause (e.g., no such file).
- 2: This means misuse of a shell builtin, corresponding to passing an invalid possibility.
- 126: Not executable
- 127: Command not discovered
- 128: Invalid argument to exit
- 128+N: Deadly error sign N. The command or script was terminated by a sign N.
- 130: Command terminated with ^C
- 255: Exit standing out of vary. The command or script tried to exit with a worth higher than 255.
Wrap-up
Poorly written scripts could carry out some motion on behalf of the individual working the script after which blithely proceed on with out ever checking whether or not the motion taken was profitable. For instance, a script would possibly echo concern a sequence of “putting in …” messages with out ever checking whether or not the assorted packages that the script was meant to put in have been out there for putting in or whether or not the installations accomplished efficiently. Others exit with a non-zero standing code at any time when one thing goes flawed, however then ignore the resultant standing code when one script calls one other. The truth is, I’ve seen some scripts which concern “efficiently put in” messages regardless of the truth that the software program set up was something however profitable.