OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Exit Status

Commands (including the scripts and shell functions we write) issue a value to the system when they terminate, called an exit status. This value, which is an integer in the range of 0 to 255, indicates the success or failure of the command’s execution. By convention, a value of zero indicates success and any other value indicates failure. The shell provides a parameter that we can use to examine the exit status. Here we see it in action:



[me@linuxbox ~]$ ls -d /usr/bin

/usr/bin

[me@linuxbox ~]$ echo $?

0

[me@linuxbox ~]$ ls -d /bin/usr

ls: cannot access /bin/usr: No such file or directory [me@linuxbox ~]$ echo $?

2

[me@linuxbox ~]$ ls -d /usr/bin

/usr/bin

[me@linuxbox ~]$ echo $?

0

[me@linuxbox ~]$ ls -d /bin/usr

ls: cannot access /bin/usr: No such file or directory [me@linuxbox ~]$ echo $?

2

Exit Status


In this example, we execute the ls command twice. The first time, the command exe- cutes successfully. If we display the value of the parameter $?, we see that it is zero. We execute the ls command a second time (specifying a non-existent directory) , producing an error, and examine the parameter $? again. This time it contains a 2, indicating that the command encountered an error. Some commands use different exit status values to provide diagnostics for errors, while many commands simply exit with a value of one when they fail. Man pages often include a section entitled “Exit Status,” describing what codes are used. However, a zero always indicates success.

The shell provides two extremely simple builtin commands that do nothing except termi- nate with either a zero or one exit status. The true command always executes success- fully and the false command always executes unsuccessfully:


[me@linuxbox ~]$ true [me@linuxbox ~]$ echo $? 0

[me@linuxbox ~]$ false [me@linuxbox ~]$ echo $? 1

[me@linuxbox ~]$ true [me@linuxbox ~]$ echo $? 0

[me@linuxbox ~]$ false [me@linuxbox ~]$ echo $? 1


We can use these commands to see how the if statement works. What the if statement really does is evaluate the success or failure of commands:



[me@linuxbox ~]$ if true; then echo "It's true."; fi

It's true.

[me@linuxbox ~]$ if false; then echo "It's true."; fi

[me@linuxbox ~]$

[me@linuxbox ~]$ if true; then echo "It's true."; fi

It's true.

[me@linuxbox ~]$ if false; then echo "It's true."; fi

[me@linuxbox ~]$


The command echo "It's true." is executed when the command following if exe- cutes successfully, and is not executed when the command following if does not execute successfully. If a list of commands follows if, the last command in the list is evaluated:


[me@linuxbox ~]$ if false; true; then echo "It's true."; fi It's true.

[me@linuxbox ~]$ if true; false; then echo "It's true."; fi [me@linuxbox ~]$

[me@linuxbox ~]$ if false; true; then echo "It's true."; fi It's true.

[me@linuxbox ~]$ if true; false; then echo "It's true."; fi [me@linuxbox ~]$


Top OS Cloud Computing at OnWorks: