[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True of the length of "STRING" is zero.
[ -n STRING ] or [ STRING ] True of the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq , -ne , -lt , -le , -gt or -ge . These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2" , respectively. "ARG1" and "ARG2" are integers.
Expressions may be combined using the following operators, listed in decreasing order of precedence:
Table 7-2. Combining expressions
Operation
Effect
[ ! EXPR ]
True if EXPR
is false.
[ ( EXPR ) ]
Returns the value of EXPR
. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]
True if both EXPR1
and EXPR2
are true.
[ EXPR1 -o EXPR2 ]
True if either EXPR1
or EXPR2
is true.
The [ (or test ) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi , the opening angular bracket should be closed after the conditions have been listed.
7.1.1.2. Commands following the then statement
The CONSEQUENT-COMMANDS list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi . It is important to remember that the then and fi are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semi-colon.
In a script, the different parts of the if statement are usually well-separated. Below, a couple of simple examples.
7.1.1.3. Checking files
The first example checks for the existence of a file:
anny ~> cat msgcheck.sh
#!/bin/bash
echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
echo
echo "...done."
anny ~> ./msgcheck.sh
This scripts checks the existence of the messages file.
Checking...
/var/log/messages exists.
...done.
7.1.1.4. Checking shell options
To add in your Bash configuration files:
# These lines will print a message if the noclobber option is set:
if [ -o noclobber ]
then
echo "Your files are protected against accidental overwriting using redirection."
fi
The

