Shell Scripting Exit Status Shell Scripting Return codes
In this Article we are going to see How an Shell commands exit with the status. Shell Scripting Exit Status / Shell Scripting Return codes are same. Exit codes are required to determine commands in scripts are run correctly. If any error there itself we can find reason.
Shell Scripting Exit Status / Shell Scripting return Codes
Every Command returns an exit status. Range of exit codes from 0 – 255
0 = Success. Other than 0 is error. To find out exact reason behind the every exit code make use of man pages or info to find meaning of exit status
Shell script exit status can be verified using special variable “$?” contains the return code of previously executed command
[ravi@ArkIT-Serv ~]$ ping 19220.127.116.11 ping: unknown host 1918.104.22.168 [ravi@ArkIT-Serv ~]$ echo $? 2
Based on command exit status we can make decisions to perform other tasks on shell scripts, example as mentioned below.
root@ArkIT-Serv:/scripts# cat returncode.sh #!/bin/bash ## Ping and know exit status of command ping -c 1 192.168.1.1 if test "$?" -eq "0" ; then echo "192.168.1.1 IP is reachable" else echo "192.168.1.1 IP is not Reachable" fi
Above one is the script which will check ping status and based on it’s return code it will print the statement. If exit status is 0 then it prints Host is reachable else Host not reachable.
root@sny-nripa:/scripts# sh -x returncode.sh + ping -c 1 192.168.1.1 PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data. --- 192.168.1.1 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms + test 1 -eq 0 + echo 192.168.1.1 IP is not Reachable 192.168.1.1 IP is not Reachable
It’s exit status 1 so statement it’s printed Host not reachable.
Using && (AND) and || (OR) Operators
By using Logical AND and OR Operators combine multiple commands to do required job.
mkdir /opt/backup && cp /important/* /opt/backup
Following command && will only executes when first command is succeed then second command will execute Or else both the commands will fail. In simple terms if first command is run with exit status zero then next command will execute. In above example if directory /opt/backup does not exists then no use of running copy command.
cp returncode.sh /tmp || cp returncode.sh /opt
Logical OR operator will execute second command when first command exit with non zero exit status. At any point one of the command will execute.
We can define exit /return code in each block of shell script from exit 0 – exit 255 to know where the script has been exited. If you did not defined any exit command in shell script than script exit status will considered as last command exit status.
shell scripting return codes Example
Defining exit code in shell script and see how it works
root@ArkIT-Serv:/scripts# sh -x rtn.sh + echo Enter Host IP Address:\c Enter Host IP Address:+ read IP 192.168.4.1 + ping -c 1 192.168.4.1 PING 192.168.4.1 (192.168.4.1) 56(84) bytes of data. 64 bytes from 192.168.4.1: icmp_seq=1 ttl=255 time=0.567 ms --- 192.168.4.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.567/0.567/0.567/0.000 ms + [ 0 -gt 0 ] + exit 0 root@ArkIT-Serv:/scripts# cat rtn.sh #!/bin/bash ## Exit code mentioned echo "Enter Host IP Address:\c" read IP ping -c 1 $IP if [ "$?" -gt "0" ]; then echo "$IP Not reachable." exit 1 fi exit 0
We are controlling exit status of our script by specifying exit command. If ping command exit status is non zero then script will print IP not reachable and exit with exit status 1 Or else script will exit with exit status 0. shell scripting exit status
All Commands returns an exit status from 0 to 255. 0 exit status is success other than zero is error. Special variable $? is used to determine command exit status.