Code |
Result |
if [ -n "$var" ] |
Returns true if the variable var is not empty |
if [ -z "$var" ] |
Returns true if the variable var is empty |
if [ "$var" = "abc" ] |
Returns true if the variable var is the string "abc". Note the quotes around $var; why are they required? |
if [ "$var" = "yes" \ -o "$var" = "no" ] |
Returns true if the variable var is the string "yes"
or the string "no". (BTW - notice anything different in this example??)
|
if [ "$var" -eq 0 ] |
Returns true if the variable var is the integer 0 |
if [ "$var" -lt 0 ] |
Returns true if the variable var is less than the integer 0 |
if [ $? -eq 0 ] |
The special variable $? means the "value returned by the last-executed command".
This if evaluates to true if the exit code (accessed with $?)
of the last-executed command was 0. In UNIX, programs return 0 to indicate
that they were successful. The exit code is the value that was returned
by the program, typically with a return or exit statement.
|
if [ $# -eq 0 ] |
Returns true if the number of arguments to the script is zero.
|
if [ -f "$var" ] |
Returns true if the variable var contains the name of a file |
if [ ! -f "$var" ] |
Returns true if the variable var does not contain the name of a
file |
if [ -d "$var" ] |
Returns true if the variable var contains the name of a directory |