10.5 Exit Codes

   

When a program terminates its execution, it returns a result code to the shell that shows the termination or exit status of the program. In the case of termination after successful execution, this code is zero. If the program terminates abnormally, the exit code is not equal to zero. The exit code of the last executed program can be checked by displaying the value of a special variable $? . See the following examples to get an idea of how the exit code works.

 $  ls  PHCO_18132.depot  phco_18132.txt    script-02 PHCO_18132.text   scr               script-03 phco_18131.txt    script-01         script-05 $  echo $?  0 $ $  mv  Usage: mv [-f] [-i] [-e warnforceignore] f1 f2        mv [-f] [-i] [-e warnforceignore] f1 ... fn d1        mv [-f] [-i] [-e warnforceignore] d1 d2 $  echo $?  1 $ 

The first command is the ls command, which executed successfully. After that, you used the echo $? command, and it showed you an exit code zero. The second command was mv . You did not provide any argument to this command, which is a must, so the command terminated abnormally and returned a value of 1.

You can use exit codes in your program to check the execution status of any commands used within the program. If a command used in the program did not execute successfully, you can make corrective measures.

You can also terminate your shell program at any point by using the exit command. The exit command can be supplied with an argument that will show the exit status of your program and can be used for debugging purposes. As an example, if you are writing a program and it needs at least one command line argument, you can terminate its execution if no argument is supplied.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net