You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
Table 7-2. Combining expressions<br />
Operation<br />
Effect<br />
[ ! EXPR ] True if EXPR is false.<br />
[ ( EXPR ) ]<br />
[ EXPR1 -a EXPR2<br />
]<br />
Returns the value of EXPR. This may be used to override the normal precedence of<br />
operators.<br />
True if both EXPR1 and EXPR2 are true.<br />
[ EXPR1 -o EXPR2<br />
True if either EXPR1 or EXPR2 is true.<br />
]<br />
The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of<br />
arguments. More information about this subject can be found in the <strong>Bash</strong> documentation. Just like the if is<br />
closed with fi, the opening square bracket should be closed after the conditions have been listed.<br />
7.1.1.2. Commands following the then statement<br />
The CONSEQUENT-COMMANDS list that follows the then statement can be any valid UNIX command,<br />
any executable program, any executable shell script or any shell statement, with the exception of the closing<br />
fi. It is important to remember that the then and fi are considered to be separated statements in the shell.<br />
Therefore, when issued on the command line, they are separated by a semi-colon.<br />
In a script, the different parts of the if statement are usually well-separated. Below, a couple of simple<br />
examples.<br />
7.1.1.3. Checking files<br />
The first example checks for the existence of a file:<br />
anny ~> cat msgcheck.sh<br />
#!/bin/bash<br />
echo "This scripts checks the existence of the messages file."<br />
echo "Checking..."<br />
if [ -f /var/log/messages ]<br />
then<br />
echo "/var/log/messages exists."<br />
fi<br />
echo<br />
echo "...done."<br />
anny ~> ./msgcheck.sh<br />
This scripts checks the existence of the messages file.<br />
Checking...<br />
/var/log/messages exists.<br />
...done.<br />
7.1.1.4. Checking shell options<br />
To add in your <strong>Bash</strong> configuration files:<br />
# These lines will print a message if the noclobber option is set:<br />
Chapter 7. Conditional statements 81