<sect1id="sect_07_01"><title>Introduction to if</title>
<sect2id="sect_07_01_01"><title>General</title>
<para>At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The <command>if</command> construction allows you to specify such conditions.</para>
<para>The most compact syntax of the <command>if</command> command is:</para>
<cmdsynopsis><command>if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi</command></cmdsynopsis>
<para>The <command>TEST-COMMAND</command> list is executed, and if its return status is zero, the <command>CONSEQUENT-COMMANDS</command> list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.</para>
<para>The <command>TEST-COMMAND</command> often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the <filename>FILE</filename> argument to one of the primaries is of the form <filename>/dev/fd/N</filename>, then file descriptor <quote>N</quote> is checked. <filename>stdin</filename>, <filename>stdout</filename> and <filename>stderr</filename> and their respective file descriptors may also be used for tests.</para>
<sect3id="sect_07_01_01_01"><title>Expressions used with if</title>
<para>The table below contains an overview of the so-called <quote>primaries</quote> that make up the <command>TEST-COMMAND</command> command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.</para>
<row><entry>[ <option>-a</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists.</entry></row>
<row><entry>[ <option>-b</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a block-special file.</entry></row>
<row><entry>[ <option>-c</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a character-special file.</entry></row>
<row><entry>[ <option>-d</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a directory.</entry></row>
<row><entry>[ <option>-e</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists.</entry></row>
<row><entry>[ <option>-f</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a regular file.</entry></row>
<row><entry>[ <option>-g</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and its SGID bit is set.</entry></row>
<row><entry>[ <option>-h</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a symbolic link.</entry></row>
<row><entry>[ <option>-k</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and its sticky bit is set.</entry></row>
<row><entry>[ <option>-p</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a named pipe (FIFO).</entry></row>
<row><entry>[ <option>-r</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is readable.</entry></row>
<row><entry>[ <option>-s</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and has a size greater than zero.</entry></row>
<row><entry>[ <option>-t</option><filename>FD</filename> ]</entry><entry>True if file descriptor <filename>FD</filename> is open and refers to a terminal.</entry></row>
<row><entry>[ <option>-u</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and its SUID (set user ID) bit is set.</entry></row>
<row><entry>[ <option>-x</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is executable.</entry></row>
<row><entry>[ <option>-O</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is owned by the effective user ID.</entry></row>
<row><entry>[ <option>-G</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is owned by the effective group ID.</entry></row>
<row><entry>[ <option>-L</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a symbolic link.</entry></row>
<row><entry>[ <option>-N</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and has been modified since it was last read.</entry></row>
<row><entry>[ <option>-S</option><filename>FILE</filename> ]</entry><entry>True if <filename>FILE</filename> exists and is a socket.</entry></row>
<row><entry>[ <filename>FILE1</filename><option>-nt</option><filename>FILE2</filename> ]</entry><entry>True if <filename>FILE1</filename> has been changed more recently than <filename>FILE2</filename>, or if <filename>FILE1</filename> exists and <filename>FILE2</filename> does not.</entry></row>
<row><entry>[ <filename>FILE1</filename><option>-ot</option><filename>FILE2</filename> ]</entry><entry>True if <filename>FILE1</filename> is older than <filename>FILE2</filename>, or is <filename>FILE2</filename> exists and <filename>FILE1</filename> does not.</entry></row>
<row><entry>[ <filename>FILE1</filename><option>-ef</option><filename>FILE2</filename> ]</entry><entry>True if <filename>FILE1</filename> and <filename>FILE2</filename> refer to the same device and inode numbers.</entry></row>
<row><entry>[ <option>-o</option> OPTIONNAME ]</entry><entry>True if shell option <quote>OPTIONNAME</quote> is enabled.</entry></row>
<row><entry>[ STRING1 == STRING2 ] </entry><entry>True if the strings are equal. <quote>=</quote> may be used instead of <quote>==</quote> for strict POSIX compliance.</entry></row>
<row><entry>[ STRING1 != STRING2 ] </entry><entry>True if the strings are not equal.</entry></row>
<row><entry>[ STRING1 < STRING2 ] </entry><entry>True if <quote>STRING1</quote> sorts before <quote>STRING2</quote> lexicographically in the current locale.</entry></row>
<row><entry>[ STRING1 > STRING2 ] </entry><entry>True if <quote>STRING1</quote> sorts after <quote>STRING2</quote> lexicographically in the current locale.</entry></row>
<row><entry>[ ARG1 OP ARG2 ]</entry><entry><quote>OP</quote> is one of <option>-eq</option>, <option>-ne</option>, <option>-lt</option>, <option>-le</option>, <option>-gt</option> or <option>-ge</option>. These arithmetic<indexterm><primary>arithmetic expansion</primary><secondary>operators</secondary></indexterm> binary operators return true if <quote>ARG1</quote> is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to <quote>ARG2</quote>, respectively. <quote>ARG1</quote> and <quote>ARG2</quote> are integers.</entry></row>
<row><entry>[ ! EXPR ]</entry><entry>True if <command>EXPR</command> is false.</entry></row>
<row><entry>[ ( EXPR ) ]</entry><entry>Returns the value of <command>EXPR</command>. This may be used to override the normal precedence of operators.</entry></row>
<row><entry>[ EXPR1 -a EXPR2 ]</entry><entry>True if both <command>EXPR1</command> and <command>EXPR2</command> are true.</entry></row>
<row><entry>[ EXPR1 -o EXPR2 ]</entry><entry>True if either <command>EXPR1</command> or <command>EXPR2</command> is true.</entry></row>
<para>The <command>[</command> (or <command>test</command>) 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 <command>if</command> is closed with <command>fi</command>, the opening square bracket should be closed after the conditions have been listed.</para>
<sect3id="sect_07_01_01_02"><title>Commands following the then statement</title>
<para>The <command>CONSEQUENT-COMMANDS</command> list that follows the <command>then</command> statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing <command>fi</command>. It is important to remember that the <command>then</command> and <command>fi</command> are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semi-colon.</para>
<para>In a script, the different parts of the <command>if</command> statement are usually well-separated. Below, a couple of simple examples.</para>
<para>However, if you use testing of conditions that depend on the environment, you might get different results when you enter the same command in a script, because the script will open a new shell, in which expected variables and options might not be set automatically.</para>
</note>
</sect3>
</sect2>
<sect2id="sect_07_01_02"><title>Simple applications of if</title>
<para>The <varname>?</varname> variable holds the exit status of the previously executed command (the most recently completed foreground process).</para>
<para>The following example shows a simple test:</para>
<para>The following example demonstrates that <command>TEST-COMMANDS</command> might be any UNIX command that returns an exit status, and that <command>if</command> again returns an exit status of zero:</para>
<para>Similar to the <quote>&&</quote> expression which indicates what to do if the test proves true, <quote>||</quote> specifies what to do if the test is false.</para>
<para>Regular expressions may also be used in comparisons:</para>
<para>Most programmers will prefer to use the <command>test</command> built-in command, which is equivalent to using square brackets for comparison, like this:</para>
<screen>
test "$(whoami)" != 'root' && (echo you are using a non-privileged account; exit 1)
<para>If you invoke the <command>exit</command> in a subshell, it will not pass variables to the parent. Use { and } instead of ( and ) if you do not want Bash to fork a subshell.</para>
<para>See the info pages for Bash for more information on pattern matching with the <quote>(( EXPRESSION ))</quote> and <quote>[[ EXPRESSION ]]</quote> constructs.</para>
</sect3>
</sect2>
</sect1>
<sect1id="sect_07_02"><title>More advanced if usage</title>
<para>This is the construct to use to take one course of action if the <command>if</command> commands test true, and another if it tests false. An example:</para>
<para>Contrary to <parameter>[</parameter>, <parameter>[[</parameter> prevents word splitting of variable values. So, if <varname>VAR="var with spaces"</varname>, you do not need to double quote <varname>$VAR</varname> in a test - eventhough using quotes remains a good habit. Also, <parameter>[[</parameter> prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames. Using <parameter>[[</parameter>, <parameter>==</parameter> and <parameter>!=</parameter> interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: <parameter>[[ "value" == val* ]]</parameter>.</para>
<para>Like the <command>CONSEQUENT-COMMANDS</command> list following the <command>then</command> statement, the <command>ALTERNATE-CONSEQUENT-COMMANDS</command> list following the <command>else</command> statement can hold any UNIX-style command that returns an exit status.</para>
<para>Another example, extending the one from <xreflinkend="sect_07_01_02_01"/>:</para>
<para>We switch to the <emphasis>root</emphasis> account to demonstrate the effect of the <command>else</command> statement - your <emphasis>root</emphasis> is usually a local account while your own user account might be managed by a central system, such as an LDAP server.</para>
</sect3>
<sect3id="sect_07_02_01_02"><title>Checking command line arguments</title>
<para>Instead of setting a variable and then executing a script, it is frequently more elegant to put the values for the variables<indexterm><primary>arguments</primary><secondary>testing</secondary></indexterm> on the command line.</para>
<para>We use the positional parameters<indexterm><primary>arguments</primary><secondary>positional parameters</secondary></indexterm><varname>$1</varname>, <varname>$2</varname>, ..., <varname>$N</varname> for this purpose. <varname>$#</varname> refers to the number of command line arguments<indexterm><primary>arguments</primary><secondary>number of arguments</secondary></indexterm>. <varname>$0</varname> refers to the name of the script.</para>
<para>The following example<indexterm><primary>arguments</primary><secondary>example test number</secondary></indexterm> shows how to change the previous script so that it prints a message if more or less than 2 arguments are given:</para>
<para>The first argument is referred to as <varname>$1</varname>, the second as <varname>$2</varname> and so on. The total number of arguments is stored in <varname>$#</varname>.</para>
<para>Check out <xreflinkend="sect_07_02_05"/> for a more elegant way to print usage messages.</para>
</sect3>
<sect3id="sect_07_02_01_04"><title>Testing that a file exists</title>
<para>This test is done in a lot of scripts<indexterm><primary>arguments</primary><secondary>example existence</secondary></indexterm>, because there's no use in starting a lot of programs if you know they're not going to work:</para>
<para>Note that the file is referred to using a variable; in this case it is the first argument to the script. Alternatively, when no arguments are given, file locations are usually stored in variables at the beginning of a script, and their content is referred to using these variables. Thus, when you want to change a file name in a script, you only need to do it once.</para>
<para>The above example will fail if the value of <varname>$1</varname> can be parsed as multiple words. In that case, the <command>if</command> command can be fixed either using double quotes around the filename, or by using <parameter>[[</parameter> instead of <parameter>[</parameter>.</para>
<para>The <command>TEST-COMMANDS</command> list is executed, and if its return status is zero, the <command>CONSEQUENT-COMMANDS</command> list is executed. If
<command>TEST-COMMANDS</command> returns a non-zero status, each <command>elif</command> list is executed in turn, and if its exit status is zero, the corresponding <command>MORE-CONSEQUENT-COMMANDS</command> is executed and the command completes. If <command>else</command> is followed by an <command>ALTERNATE-CONSEQUENT-COMMANDS</command> list, and the final command in the final <command>if</command> or <command>elif</command> clause has a non-zero exit status, then <command>ALTERNATE-CONSEQUENT-COMMANDS</command> is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.</para>
<para>Inside the <command>if</command> statement, you can use another <command>if</command> statement. You may use as many levels of nested <command>if</command>s as you can logically manage.</para>
<para>We use the double brackets for testing<indexterm><primary>arithmetic expression</primary><secondary>testing</secondary></indexterm> an arithmetic expression, see <xreflinkend="sect_03_04_05"/>. This is equivalent to the <command>let</command> statement. You will get stuck using square brackets here, if you try something like <command>$[$year % 400]</command>, because here, the square brackets don't represent an actual command by themselves.</para>
<para>Among other editors, <command>gvim</command> is one of those supporting colour schemes according to the file format; such editors are useful for detecting errors in your code.</para>
<para>We already briefly met the <command>exit</command> statement in <xreflinkend="sect_07_02_01_03"/>. It terminates execution of the entire script. It is most often used if the input requested from the user is incorrect, if a statement did not run successfully or if some other error occurred.</para>
<para>The <command>exit</command> statement takes an optional argument<indexterm><primary>arguments</primary><secondary>exit status</secondary></indexterm>. This argument is the integer exit status code, which is passed back to the parent and stored in the <varname>$?</varname> variable.</para>
<para>A zero argument<indexterm><primary>exit status</primary><secondary>arguments</secondary></indexterm> means that the script ran successfully. Any other value may be used by programmers to pass back different messages to the parent, so that different actions can be taken according to failure or success of the child process. If no argument is given to the <command>exit</command> command, the parent shell uses the current value of the <varname>$?</varname> variable.</para>
<para>Below is an example with a slightly adapted <filename>penguin.sh</filename> script, which sends its exit status back to the parent, <filename>feed.sh</filename>:</para>
<para>As you can see, exit status codes can be chosen freely. Existing commands usually have a series of defined codes; see the programmer's manual for each command for more information.</para></sect2>
</sect1>
<sect1id="sect_07_03"><title>Using case statements</title>
<para>Nested <command>if</command> statements might be nice, but as soon as you are confronted with a couple of different possible actions to take, they tend to confuse. For the more complex conditionals, use the <command>case</command> syntax:</para>
<para>Each case is an expression matching a pattern. The commands in the <command>COMMAND-LIST</command> for the first match are executed. The <quote>|</quote> symbol is used for separating multiple patterns, and the <quote>)</quote> operator terminates a pattern list. Each case plus its according commands are called a <emphasis>clause</emphasis>. Each clause must be terminated with <quote>;;</quote>. Each <command>case</command> statement is ended with the <command>esac</command> statement.</para>
<para>In the example, we demonstrate use of cases for sending a more selective warning message with the <filename>disktest.sh</filename> script:</para>
Start thinking about cleaning out some stuff. There's a partition that is 87 % full.
<prompt>anny ~/testdir></prompt>
</screen>
<para>Of course you could have opened your mail program to check the results; this is just to demonstrate that the script sends a decent mail with <quote>To:</quote>, <quote>Subject:</quote> and <quote>From:</quote> header lines.</para>
<para>Many more examples using <command>case</command> statements can be found in your system's init script directory. The startup scripts use <command>start</command> and <command>stop</command> cases to run or stop system processes. A theoretical example can be found in the next section.</para>
<para>Initscripts often make use of <command>case</command> statements for starting, stopping and querying system services. This is an excerpt of the script that starts <application>Anacron</application>, a daemon that runs commands periodically with a frequency specified in days.</para>
<para>The tasks to execute in each case, such as stopping and starting the daemon, are defined in functions, which are partially sourced from the <filename>/etc/rc.d/init.d/functions</filename> file. See <xreflinkend="chap_11"/> for more explanation.</para>
<para>In this chapter we learned how to build conditions into our scripts so that different actions can be undertaken upon success or failure of a command. The actions can be determined using the <command>if</command> statement. This allows you to perform arithmetic and string comparisons, and testing of exit code, input and files needed by the script.</para>
<para>A simple <command>if/then/fi</command> test often preceeds commands in a shell script in order to prevent output generation, so that the script can easily be run in the background or through the <application>cron</application> facility. More complex definitions of conditions are usually put in a <command>case</command> statement.</para>
<para>Upon successful condition testing, the script can explicitly inform the parent using the <command>exit 0</command> status. Upon failure, any other number may be returned. Based on the return code, the parent program can take appropriate action.</para>
<para>Here are some ideas to get you started using <command>if</command> in scripts:</para>
<orderedlist>
<listitem><para>Use an <command>if/then/elif/else</command> construct that prints information about the current month. The script should print the number of days in this month, and give information about leap years if the current month is February.</para></listitem>
<listitem><para>Do the same, using a <command>case</command> statement and an alternative use of the <command>date</command> command.</para></listitem>
<listitem><para>Modify <filename>/etc/profile</filename> so that you get a special greeting message when you connect to your system as <emphasis>root</emphasis>.</para></listitem>
<listitem><para>Edit the <filename>leaptest.sh</filename> script from <xreflinkend="sect_07_02_04"/> so that it requires one argument, the year. Test that exactly one argument is supplied.</para></listitem>
<listitem><para>Write a script called <filename>whichdaemon.sh</filename> that checks if the <command>httpd</command> and <command>init</command> daemons are running on your system. If an <command>httpd</command> is running, the script should print a message like, <quote>This machine is running a web server.</quote> Use <command>ps</command> to check on processes.</para></listitem>
<listitem><para>Write a script that makes a backup of your home directory on a remote machine using <command>scp</command>. The script should report in a log file, for instance <filename>~/log/homebackup.log</filename>. If you don't have a second machine to copy the backup to, use <command>scp</command> to test copying it to the localhost. This requires SSH keys between the two hosts, or else you have to supply a password. The creation of SSH keys is explained in <command>man <parameter>ssh-keygen</parameter></command>.</para>
<listitem><para>Adapt the script from the first example in <xreflinkend="sect_07_03_01"/> to include the case of exactly 90% disk space usage, and lower than 10% disk space usage.</para></listitem>
<para>The script should use <command>tar <option>cf</option></command> for the creation of the backup and <command>gzip</command> or <command>bzip2</command> for compressing the <filename>.tar</filename> file. Put all filenames in variables. Put the name of the remote server and the remote directory in a variable. This will make it easier to re-use the script or to make changes to it in the future.</para>
<para>The script should check for the existence of a compressed archive. If this exists, remove it first in order to prevent output generation.</para>
<para>The script should also check for available diskspace. Keep in mind that at any given moment you could have the data in your home directory, the data in the <filename>.tar</filename> file and the data in the compressed archive all together on your disk. If there is not enough diskspace, exit with an error message in the log file.</para>
<para>The script should clean up the compressed archive before it exits.</para></listitem>