7. Variablen-Tests

7.1 Zahlenvergleich mit anschließender Programmverzweigung

7.2 Vergleich

["abc" != "xyz"] ; echo $?

7.3 String is not null

test -n "$DISPLAY" ; echo $?test -n "$DISPLAY" && echo"OK"

Mit IF

if["$a" -gt "0"]; thenecho'true'elseecho'false'fi

7.4 Integer comparison

-eq is equal to if["$a" -eq "$b"]
-ne is not equal toif["$a" -ne "$b"]
-gt is greater thanif["$a" -gt "$b"]
-ge is greater than or equal toif["$a" -ge "$b"]
-lt is less thanif["$a" -lt "$b"]
-le is less than or equal toif["$a" -le "$b"]
< is less than (within double parentheses)(("$a" < "$b"))
<= is less than or equal to (within double parentheses)(("$a" <= "$b"))
> is greater than (within double parentheses)(("$a" > "$b"))
>= is greater than or equal to (within double parentheses)(("$a" >= "$b"))

7.5 String comparison

= is equal toif["$a" = "$b"]
== is equal toif["$a" == "$b"]
!= is not equal toif["$a" != "$b"]
This operator uses pattern matching within a [[ ... ]] construct.
< is less than, in ASCII alphabetical orderif[["$a" < "$b"]]if["$a" < "$b"]
Note that the "<" needs to be escaped within a [] construct.
> is greater than, in ASCII alphabetical orderif[["$a" > "$b"]]if["$a" > "$b"]
Note that the ">" needs to be escaped within a [] construct.
See Example 26-11for an application of this comparison operator.
-z string is "null", that is, has zero length
-n string is not "null".

7.6 Compound comparison

7.7 Multiple conditions

Using the [[ ... ]]test construct, rather than [ ... ] can prevent many logic
errors in scripts. For example,the &&, ||, <, and > operators work within
a [[]]test, despite giving an error within a [] construct.

Example:[[$umlA -le $umlN && -n $umlN]]

8. File tests

-e file exists
-a file exists
This is identical in effect to -e. It has been "deprecated," and its use is discouraged.
-f file is a regular file(not a directory or device file)
-s file is not zero size
-d file is a directory
-b file is a block device (floppy, cdrom, etc.)
-c file is a character device (keyboard, modem, sound card, etc.)
-p file is a pipe
-h file is a symbolic link
-L file is a symbolic link
-S file is a socket
-t file(descriptor) is associated with a terminal device
This test option may be used to check whether the stdin ([ -t 0]) or stdout ([ -t 1])in
a given script is a terminal.
-r file has read permission (for the user running the test)
-wfile has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id(sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be usefulfor a directory shared by a workgroup.
-u set-user-id(suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was lastread
-nt f1 -nt f2file f1 is newer than f2
-ot f1 -ot f2file f1 is older than f2
-ef f1 -ef f2
files f1 and f2 are hard links to the same file
! "not" -- reverses the sense of the tests above (returns trueif condition absent).