Changes

Jump to: navigation, search

BASH Exit Status

899 bytes added, 01:36, 16 September 2008
The test ([) Command
|Integer
|Equal
|$x -eq 4
|-
|Integer
|Not equal
|$x -ne 4
|-
|Integer
|Greater than
|$x -gt 0
|-
|Integer
|Less than
|$x -lt 1000
|-
|Integer
|Greater than or equal to
|$x -ge $y
|}
 
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 =

Navigation menu