Difference between revisions of "BASH Exit Status"

From CDOT Wiki
Jump to: navigation, search
(Setting the Exit Status when Exiting from a Shell Script)
(The test ([) Command)
Line 43: Line 43:
 
|Integer
 
|Integer
 
|Equal
 
|Equal
|$x -eq 4
+
| $x -eq 4
  
 
|-
 
|-
Line 49: Line 49:
 
|Integer
 
|Integer
 
|Not equal
 
|Not equal
|$x -ne 4
+
| $x -ne 4
  
 
|-
 
|-
Line 55: Line 55:
 
|Integer
 
|Integer
 
|Greater than
 
|Greater than
|$x -gt 0
+
| $x -gt 0
  
 
|-
 
|-
Line 61: Line 61:
 
|Integer
 
|Integer
 
|Less than
 
|Less than
|$x -lt 1000
+
| $x -lt 1000
  
 
|-
 
|-
Line 67: Line 67:
 
|Integer
 
|Integer
 
|Greater than or equal to
 
|Greater than or equal to
|$x -ge $y
+
| $x -ge $y
  
  
Line 89: Line 89:
  
 
|}
 
|}
 +
 +
There are also a number of unary file tests, including:
 +
 +
{|class="mediawiki" border="1"
 +
 +
|-
 +
!Operator
 +
!Test
 +
!Example
 +
 +
|-
 +
|<nowiki>-e</nowiki>
 +
|File exists
 +
|-e /etc/passwd
 +
 +
|-
 +
|<nowiki>-r</nowiki>
 +
|File is readable
 +
|-r /etc/hosts
 +
 +
|-
 +
|<nowiki>-w</nowiki>
 +
|File is writable
 +
|-w /tmp
 +
 +
|-
 +
|<nowiki>-x</nowiki>
 +
|File is executable
 +
|-x /usr/bin/ls
 +
 +
|-
 +
|<nowiki>-f</nowiki>
 +
|File is a regular file
 +
|-f /dev/tty
 +
 +
|-
 +
 +
|<nowiki>-d</nowiki>
 +
|File is a directory
 +
|-d /dev/tty
 +
 +
|}
 +
 +
These tests and comparisons are used with the <code>test</code> or <code>[</code> 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 and Flow Control =

Revision as of 01:36, 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 or [ 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.