Difference between revisions of "BASH Exit Status"

From CDOT Wiki
Jump to: navigation, search
(Exit Codes and Flow Control)
(Setting the Exit Status when Exiting from a Shell Script)
Line 26: Line 26:
  
 
Will exit the script with an exit code of 2.
 
Will exit the script with an exit code of 2.
 +
 +
= 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:
 +
 +
{|class="mediawiki" border="1"
 +
 +
|-
 +
!Operator
 +
!Comparision type
 +
!Comparison
 +
!Example
 +
 +
|-
 +
|<nowiki>-eq</nowiki>
 +
|Integer
 +
|Equal
 +
|$x -eq 4
 +
 +
|-
 +
|<nowiki>-ne</nowiki>
 +
|Integer
 +
|Not equal
 +
|$x -ne 4
 +
 +
|-
 +
|<nowiki>-gt</nowiki>
 +
|Integer
 +
|Greater than
 +
|$x -gt 0
 +
 +
|-
 +
|<nowiki>-lt</nowiki>
 +
|Integer
 +
|Less than
 +
|$x -lt 1000
 +
 +
|-
 +
|<nowiki>-ge</nowiki>
 +
|Integer
 +
|Greater than or equal to
 +
|$x -ge $y
 +
 +
 +
|-
 +
|<nowiki>-le</nowiki>
 +
|Integer
 +
|Less than or equal to
 +
|$x -le 96
 +
 +
|-
 +
|=
 +
|String
 +
|Equal
 +
|"$x" = "Y"
 +
 +
|-
 +
|!=
 +
|String
 +
|Not equal
 +
|"$x" != "NEVER"
 +
 +
|}
  
 
= Exit Codes and Flow Control =
 
= Exit Codes and Flow Control =

Revision as of 01:28, 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"

Exit Codes and Flow Control

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