Difference between revisions of "BASH Exit Status"

From CDOT Wiki
Jump to: navigation, search
(The test ([) Command)
(The test ([) Command)
Line 27: Line 27:
 
Will exit the script with an exit code of 2.
 
Will exit the script with an exit code of 2.
  
= The test ([) Command =
+
= The test Command =
  
 
BASH has a built-in test command (similar to <code>/bin/test</code>) which can perform basic string and integer comparisons using these operators:
 
BASH has a built-in test command (similar to <code>/bin/test</code>) which can perform basic string and integer comparisons using these operators:
Line 132: Line 132:
 
|}
 
|}
  
These tests and comparisons are used with the <code>test</code> or <code>[</code> command:
+
These tests and comparisons are used with the <code>test</code> command or the synonym, <code>[</code> command:
  
 
  $ test 10 -gt 5
 
  $ test 10 -gt 5

Revision as of 00:37, 16 September 2008

Exist Status

Each process that executes on a Linux system leaves a whole number exit code (or exit status, result code, or error code) when it terminates.

The value of the exit code is usually 0 if no errors were encountered, or non-zero if an error was encountered. The meaning of specific codes varies program-by-program; see the documentation (such as the man page) for a program to determine the meaning of those codes.

In a multi-command pipeline, the exit status of the last command is used as the exit status for the entire pipeline. (In BASH documentation, the term pipeline is used to refer to any single command or sequence of commands; see the BASH manpage for details).

Retrieving Exist Status

BASH places the exit status of the most recently-executed pipeline in the special variable $?. You can view the value of this variable using the echo command:

$ ls /tmp >/dev/null
$ echo $?
0
$ ls /temp >/dev/null
ls: cannot access /temp: No such file or directory
$ echo $?
2

Setting the Exit Status when Exiting from a Shell Script

The BASH exit command can be used with a whole-number numeric positional argument to exit a script with a particular status code. For example:

exit 2

Will exit the script with an exit code of 2.

The test Command

BASH has a built-in test command (similar to /bin/test) which can perform basic string and integer comparisons using these operators:

Operator Comparision type Comparison Example
-eq Integer Equal $x -eq 4
-ne Integer Not equal $x -ne 4
-gt Integer Greater than $x -gt 0
-lt Integer Less than $x -lt 1000
-ge Integer Greater than or equal to $x -ge $y


-le Integer Less than or equal to $x -le 96
= String Equal "$x" = "Y"
!= String Not equal "$x" != "NEVER"

There are also a number of unary file tests, including:

Operator Test Example
-e File exists
-r File is readable
-w File is writable
-x File is executable
-f File is a regular file
-d File is a directory

These tests and comparisons are used with the test command or the synonym, [ command:

$ test 10 -gt 5
$ echo $?
0
$ test 10 -lt 5
$ echo $?
1
$ [ 10 -lt 5 ]
$ echo $?
1
$ [ -f /etc/passwd ]
$ echo $?
0
$ [ -w /etc/passwd ]
$ echo $?
1

Tests can be combined with -o (or) and -a (and), and negated with !

$ a=500
$ [ "$a" -ge 100 -a "$a" -le 1000 ]
$ echo $?
0
$ [ ! "a" = "b" ]
$ echo $?
0

Exit Codes and Flow Control

Exit codes are used extensively with the BASH Flow Control operators.