Understanding exit codes on Linux



#!/bin/bash

checkParms

if [ $? != 0 ]; then
echo Exiting: checkParms failed
exit 1
fi

Keep in mind that the exit code when a script ends depends on the last command that is run. The script below would end with an exit code of 0 if the exit command weren’t included since the if/then command doesn’t generate any errors.

#!/bin/bash

touch /tmp/log 2> /dev/null

if [ $? -eq 0 ]
then
echo "File successfully created"
else
echo "Could not create file"
exit 1
fi

Exit code numeric range

The range of values for exit codes runs from 0 to 255. If you issue an “exit 256” command in a script and then check the exit status, you’ll notice that it’s equal to 0! Since the exit code is only going to occupy a single byte, you can think of it as a modulo 256. “Exit 258”, similarly, will leave you with an exit code of 2.

Other special exit codes include 2 (meant to indicate the misuse of a shell builtin), 127 (command not found), 128 (invalid exit code), a whole bunch of exit codes above 128 (128 + a signal for fatal errors) and 130 (control C fatal error). Exit code 1, on the other hand, is a catchall for various errors. If you want to define exit codes to indicate various problems in your own scripts, you’re probably better off using codes between 3 and 125 or, as has been suggested by some contributors to the furtherance of good return code usage, 64 to 113. In either case, you have a wide range of possible return codes at your disposal. Standardizing on a set that suits your needs might streamline your coding a bit.

Here’s a list of exit codes and the types of errors they represent:

  • 0: Success. The command or script executed without any error.
  • 1: General error. The command or script failed for some unspecified reason (e.g., no such file).
  • 2:  This indicates misuse of a shell builtin, such as passing an invalid option.
  • 126: Not executable
  • 127: Command not found
  • 128: Invalid argument to exit
  • 128+N: Fatal error signal N. The command or script was terminated by a signal N.
  • 130: Command terminated with ^C
  • 255: Exit status out of range. The command or script tried to exit with a value greater than 255.

Wrap-up

Poorly written scripts may perform some action on behalf of the person running the script and then blithely continue on without ever checking whether the action taken was successful. For example, a script might echo issue a sequence of “installing …” messages without ever checking whether the various packages that the script was intended to install were available for installing or whether the installations completed successfully. Others exit with a non-zero status code whenever something goes wrong, but then ignore the resultant status code when one script calls another. In fact, I have seen some scripts which issue “successfully installed” messages in spite of the fact that the software installation was anything but successful.



Source link

case studies

See More Case Studies

Contact us

Partner with Us for Comprehensive IT

We’re happy to answer any questions you may have and help you determine which of our services best fit your needs.

Your benefits:
What happens next?
1

We Schedule a call at your convenience 

2

We do a discovery and consulting meting 

3

We prepare a proposal 

Schedule a Free Consultation