

shell scripting exit status ConclusionĪll Commands returns an exit status from 0 to 255. 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. We are controlling exit status of our script by specifying exit command. shell scripting return codes Exampleĭefining exit code in shell script and see how it works sh -x rtn.sh If you did not defined any exit command in shell script than script exit status will considered as last command exit status. 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. At any point one of the command will execute. Logical OR operator will execute second command when first command exit with non zero exit status. cp returncode.sh /tmp || cp returncode.sh /opt In above example if directory /opt/backup does not exists then no use of running copy command. In simple terms if first command is run with exit status zero then next command will execute. mkdir /opt/backup & cp /important/* /opt/backupįollowing command & will only executes when first command is succeed then second command will execute Or else both the commands will fail. Using & (AND) and || (OR) Operatorsīy using Logical AND and OR Operators combine multiple commands to do required job. It’s exit status 1 so statement it’s printed Host not reachable. If exit status is 0 then it prints Host is reachable else Host not reachable. cat returncode.shĪbove one is the script which will check ping status and based on it’s return code it will print the statement. Shell script exit status can be verified using special variable “$?” contains the return code of previously executed command ~]$ ping 1923.6.3.2īased on command exit status we can make decisions to perform other tasks on shell scripts, example as mentioned below. To find out exact reason behind the every exit code make use of man pages or info to find meaning of exit status Range of exit codes from 0 – 255Ġ = Success. Shell Scripting Exit Status / Shell Scripting return CodesĮvery Command returns an exit status. If any error there itself we can find reason. Exit codes are required to determine commands in scripts are run correctly. Shell Scripting Exit Status / Shell Scripting Return codes are same. In this Article we are going to see How an Shell commands exit with the status.
