If / else statements on bash script
An overview of if/else statements in Bash scripts and comparison conditions
Bash provides the following forms of if/else statements:
-
Using if for a basic comparison
if [ condition ] then ... fi -
Using if and else for comparisons
if [ condition ] then ... else ... fi -
Using if, elif (else if), and else for comparisons
if [ condition ] then ... elif [ condition ] then ... else ... fi
Conditions can test many things. The available comparisons are summarized below.
String comparison operations
| String Comparison | Description |
| Str1 == Str2 | Returns true if the strings are equal |
| Str1 != Str2 | Returns true if the strings are not equal |
| -n Str1 | Returns true if the string is not null |
| -z Str1 | Returns true if the string is null |
Numeric comparison operations
| Numeric Comparison | Description |
| expr1 -eq expr2 | Returns true if the expressions are equal |
| expr1-ne expr2 | Returns true if the expressions are not equal |
| expr1 -gt expr2 | Returns true if expr1 is greater than expr2 |
| expr1 -ge expr2 | Returns true if expr1 is greater than or equal to expr2 |
| expr1 -lt expr2 | Returns true if expr1 is less than expr2 |
| expr1 -le expr2 | Returns true if expr1 is less than or equal to expr2 |
| expr1 -a expr2 | True if both expr1 and expr2 are true. |
| expr1 -o expr2 | True if either expr1 or expr2 is true. |
| ! expr1 | Negates the result of the expression |
File test operations
| File Conditionals | Description |
| -d dir | True if the file is a directory |
| -e file -a file | True if the file exists (note that this is not particularly portable, thus -f is generally used) |
| -f file | True if the provided string is a file |
| -g file | True if the group id is set on a file |
| -k file | True if file exists and “sticky” bit is set |
| -r file | True if the file is readable |
| -s file | True if the file has a non-zero size (size greater than zero) |
| -b file | True if file exists and is a block special file |
| -c file | True if file exists and is a character special file |
| -p file | True if file exists and is a named pipe (FIFO) |
| -t [fd] | True if file descriptor fd is open and refers to a terminal. If fd is omitted, it defaults to 1 (standard output). |
| -u file | True if the user id is set on a file |
| -w file | True if the file is writable |
| -x file | True if the file is an executable |
| -O file | True if file exists and is owned by the effective user id |
| -G file | True if file exists and is owned by the effective group id |
| -L file | True if file exists and is a symbolic link |
| -S file | True if file exists and is a socket. |
| -N file | True if file exists and has been modified since it was last read. |
| FILE1 -nt FILE2 | True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. |
| FILE1 -ot FILE2 | True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not. |
| FILE1 -ef FILE2 | True if FILE1 and FILE2 refer to the same device and inode numbers. |