<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article id="UnixScripting">
  <articleinfo>
    <title>Unix Shell Scripting Tutorial</title>
    <author>
      <firstname>Ashley</firstname>
      <othername>J.S</othername>
      <surname>Mills</surname>
      <affiliation>
        <address><email>ashley@ashleymills.com</email></address>
      </affiliation>
    </author>

    <copyright>
      <year>2005</year>
      <holder role="mailto:ashley@ashleymills.com">The University Of Birmingham</holder>
    </copyright>
  </articleinfo>

  <sect1 id="UnixScripting-Introduction"><title>Introduction</title>
    <para>
      Time is precious.  It is non-sense-ical to waste time typing a frequently used sequence of commands at a command prompt, more especially if they are abnormally long or complex.  Scripting is a way by which one can alleviate this necessity by automating these command sequences in order to make ones life at the shell easier and more productive.  Scripting is all about making the computer, <emphasis role="strong">the tool</emphasis>, do the work.  Hopefully by the end of this tutorial you should have a good idea of the kinds of scripting languages available for Unix and how to apply them to your problems.
    </para>
  </sect1>

  <sect1 id="UnixScripting-Environment"><title>Environment</title>
    <para>
      In order to peruse this tutorial with ease it is probably necessary to know a little bit about how Unix works, if you are new to Unix you should read the documentation: <ulink url="../unixenvars/unixenvarshome.html"><citetitle>Configuring A Unix Working Environment</citetitle></ulink>, if you are not new to Unix you should browse it to check that you know everything it discusses.
    </para>

    <para>
      Unix contains many wonderful and strange commands that can be very useful in the world of scripting, the more of the tools you know and the better you know them, the more use you will find for them in your scripting.  Most of the Unix commands and many of the builtin commands have man pages, <emphasis>man</emphasis> pages contain the usage instructions and other stuff pertaining to the parent tool.  They are not always very clear and may require reading several times. In order to access a man page in Unix the following command sequence is applied:
   </para>

   <screen><userinput><command>man</command> <replaceable>command</replaceable></userinput></screen>

   <para>
     If a man page exists for the command specified the internal viewer will be invoked and you will be able to read about the various options and usage instructions etc.
   </para>
  </sect1> 

  <sect1 id="UnixScripting-ShellScripting"><title>Shell Scripting</title>
    <sect2 id="UnixScripting-ShellScripting-Introduction"><title>Shell Scripting Introduction</title>
      <para>
        Unix uses shells to accept commands given by the user, there are quite a few different shells available.  The most commonly used shells are <acronym>SH</acronym>(Bourne SHell) <acronym>CSH</acronym>(C SHell) and <acronym>KSH</acronym>(Korn SHell), most of the other shells you encounter will be variants of these shells and will share the same syntax, <acronym>KSH</acronym> is based on <acronym>SH</acronym> and so is <acronym>BASH</acronym>(Bourne again shell).  <acronym>TCSH</acronym>(Extended C SHell) is based on <acronym>CSH</acronym>.
      </para>

      <para>
        The various shells all have built in functions which allow for the creation of shell scripts, that is, the stringing together of shell commands and constructs to automate what can be automated in order to make life easier for the user.
      </para>

      <para>
        With all these different shells available, what shell should we script in? That is debatable.  For the purpose of this tutorial we will be using <acronym>SH</acronym> because it is practically guaranteed to be available on most Unix systems you will encounter or be supported by the <acronym>SH</acronym> based shells. Your default shell may not be <acronym>SH</acronym>.  Fortunately we do not have to be using a specific shell in order to exploit its features because we can specify the shell we want to interpret our shell script within the script itself by including the following in the first line.
      </para>

      <programlisting>
#!<filename>/path/to/shell</filename>
      </programlisting>

      <para>
        Usually anything following (#) is interpreted as a comment and ignored but if it occurs on the first line with a (!) following it is treated as being special and the filename following the (!) is considered to point to the location of the shell that should interpret the script.
      </para>

      <para>
        When a script is &quot;executed&quot; it is being interpreted by an invocation of the shell that is running it. Hence the shell is said to be running non-interactively, when the shell is used &quot;normally&quot; it is said to be running interactively.
      </para>

      <note>
        <para>
          There are many variations on the basic commands and extra information which is too specific to be mentioned in this short tutorial, you should read the man page for your shell to get a more comprehensive idea of the options available to you.  This tutorial will concentrate on highlighting the most often used and useful commands and constructs. 
        </para>
      </note>
    </sect2>

    <sect2 id="UnixScripting-Shell-Scripting-Basics"><title>Shell Scripting Basics</title>
      <sect3 id="UnixScripting-Command-Redirection-And-Pipelines"><title>Command Redirection and Pipelines</title>
        <para>
          By default a normal command accepts input from standard input, which we abbreviate to stdin, standard input is the command line in the form of arguments passed to the command.  By default a normal command directs its output to standard output, which we abbreviate to stdout, standard output is usually the console display. For some commands this may be the desired action but other times we may wish to get our input for a command from somewhere other than stdin and direct our output to somewhere other than stdout.  This is done by redirection:
        </para>

        <itemizedlist>
          <listitem>
            <para>
              We use <command>&gt;</command> to redirect stdout to a file, for instance, if we wanted to redirect a directory listing generated by the <command>ls</command> we could do the following:
            </para>

            <screen><userinput><command>ls</command> <command>&gt;</command> <replaceable>file</replaceable></userinput></screen>
          </listitem>

          <listitem>
            <para>
              We use <command>&lt;</command> to specify that we want the command immediately before the redirection symbol to get its input from the source specified immediately after the symbol, for instance, we could redirect the input to <command>grep</command>(which searches for strings within files) so that it comes from a file like this: 
            </para>

            <screen><userinput><command>grep</command> <replaceable>searchterm</replaceable> <command>&lt;</command> <replaceable>file</replaceable></userinput></screen>
          </listitem>

          <listitem>
            <para>
              We use <command>&gt;&gt;</command> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from <command>date</command> like so:
            </para>

            <screen><userinput><command>date</command> <command>&gt;&gt;</command> <replaceable>file</replaceable></userinput></screen>
          </listitem>

          <listitem>
            <para>
              One can redirect standard error (stderr) to a file by using <command>2&gt;</command>, if we wanted to redirect the standard error from commandA to a file we would use:
            </para>

            <screen><userinput><command>commmandA</command> <command>2&gt;</command></userinput></screen>
          </listitem>
        </itemizedlist>

        <para>
          Pipelines are another form of redirection that are used to chain commands so that powerful composite commands can be constructed, the pipe symbol <emphasis role="strong">'|'</emphasis> takes the stdout from the command preceding it and redirects it to the command following it:
        </para>

        <screen>
          <userinput><command>ls</command> <option>-l</option> <command>|</command> <command>grep</command> <replaceable>searchword</replaceable> <command>|</command> <command>sort</command> <option>-r</option></userinput>
        </screen>

        <para>
          The example above firsts requests a long (<option>-l</option> directory listing of the current directory using the <command>ls</command> command, the output from this is then piped to <command>grep</command> which filters out all the listings containing the searchword and then finally pipes this through to <command>sort</command> which then sorts the output in reverse (<option>-r</option>, <command>sort</command> then passes the output on normally to stdout.
        </para>
      </sect3>

      <sect3 id="UnixScripting-Variables"><title>Variables</title>
         <sect4 id="UnixScripting-Variables-Internal"><title>Variables</title>
           <para>
             When a script starts all environment variables are turned into shell variables. New variables can be instantiated like this: 
           </para>

           <programlisting>
<replaceable>name</replaceable>=<replaceable>value</replaceable>
           </programlisting>

           <para>
             You must do it exactly like that, with no spaces either side of the equals sign, the name must only be made up of alphabetic characters, numeric characters and underscores, it cannot begin with a numeric character.  You should avoid using keywords like <emphasis>for</emphasis> or anything like that, the interpreter will let you use them but doing so can lead to obfuscated code ;)
           </para>

           <para>
             Variables are referenced like this: <emphasis role="strong">$</emphasis><replaceable>name</replaceable>, here is an example:
           </para>

           <programlisting>
#!/bin/sh
msg1=Hello
msg2=There!
echo $msg1 $msg2
           </programlisting>

           <para>
             This would echo &quot;Hello There!&quot; to the console display, if you want to assign a string to a variable and the string contains spaces you should enclose the string in double quotes (&quot;), the double quotes tell the shell to take the contents literally and ignore keywords, however, a few keywords are still processed.  You can still use <emphasis role="strong">$</emphasis> within a (&quot;) quoted string to include variables:
           </para>

           <programlisting>
#!/bin/sh
msg1=&quot;one&quot;
msg2=&quot;$msg1 two&quot;
msg3=&quot;$msg2 three&quot;
echo $msg3
           </programlisting>

           <para>
             Would echo &quot;one two three&quot; to the screen. The escape character can also be used within a double quoted section to output special characters, the escape character is <emphasis role="strong">&quot;\&quot;</emphasis>, it outputs the character immediately following it literally so <emphasis>\\</emphasis> would output <emphasis>\</emphasis>. A special case is when the escape character is followed by a newline, the shell ignores the newline character which allows the spreading of long commands that must be executed on a single line in reality over multiple lines within the script. The escape character can be used anywhere else too. Except within single quotes.
           </para>

           <para>
             Surrounding anything within single quotes causes it to be treated as literal text that is it will be passed on exactly as intended, this can be useful for sending command sequences to other files in order to create new scripts because the text between the single quotes will remain untouched. For example:
           </para>

           <programlisting>
#!/bin/sh
echo 'msg=&quot;Hello World!&quot;' &gt; hello
echo 'echo $msg' &gt;&gt; hello
chmod 700 hello
./hello
           </programlisting>

           <para>
             This would cause &quot;msg=&quot;Hello World!&quot; to be echoed and redirected to the file <filename>hello</filename>, &quot;echo $msg&quot; would then be echoed and redirected to the file <filename>hello</filename> but this time appended to the end. The <emphasis>chmod</emphasis> line changes the file permissions of <filename>hello</filename> so that we can execute it. The final line executes <command>hello</command> causing it output &quot;Hello World&quot;. If we had not used literal quotes we never would have had to use escape characters to ensure that ($) and (&quot;) were echoed to the file, this makes the code a little clearer. 
           </para>

           <para>
             A variable may be referenced like so <emphasis role="strong">${VARIABLENAME}</emphasis>, this allows one to place characters immediately preceding the variable like <emphasis>${VARIABLENAME}<emphasis>aaa</emphasis></emphasis> without the shell interpreting <emphasis role="strong">aaa</emphasis> as being part of the variable name.
           </para>
         </sect4>
         <sect4 id="UnixScripting-Variables-CommandLine"><title>Command Line Arguments</title>
           <para>
             Command line arguments are treated as special variables within the script, the reason I am calling them variables is because they can be changed with the <command>shift</command> command.  The command line arguments are enumerated in the following manner <emphasis>$0</emphasis>, <emphasis>$1</emphasis>, <emphasis>$2</emphasis>, <emphasis>$3</emphasis>, <emphasis>$4</emphasis>,  <emphasis>$5</emphasis>,  <emphasis>$6</emphasis>, <emphasis>$7</emphasis>, <emphasis>$8</emphasis> and <emphasis>$9</emphasis>. <emphasis>$0</emphasis> is special in that it corresponds to the name of the script itself.  <emphasis>$1</emphasis> is the first argument, <emphasis>$2</emphasis> is the second argument and so on.  To reference after the ninth argument you must enclose the number in brackets like this <emphasis>${nn}</emphasis>. You can use the <command>shift</command> command to shift the arguments 1 variable to the left so that  <emphasis>$2</emphasis> becomes <emphasis>$1</emphasis>, <emphasis>$1</emphasis> becomes <emphasis>$0</emphasis> and so on, <emphasis>$0</emphasis> gets scrapped because it has nowhere to go, this can be useful to process all the arguments using a loop, using one variable to reference the first argument and <command>shifting</command> until you have exhausted the arguments list.
           </para>

           <para>
             As well as the commandline arguments there are some special builtin variables:
           </para>

           <itemizedlist>
             <listitem>
               <para>
                 <emphasis>$#</emphasis> represents the parameter count. Useful for controlling loop constructs that need to process each parameter. 
               </para>
             </listitem>
             <listitem>
               <para>
                 <emphasis>$@</emphasis> expands to all the parameters separated by spaces. Useful for passing all the parameters to some other function or program.
               </para>
             </listitem>
             <listitem>
               <para>
                 <emphasis>$-</emphasis> expands to the flags(options) the shell was invoked with. Useful for controlling program flow based on the flags set.
               </para>
             </listitem>
             <listitem>
               <para>
                 <emphasis>$$</emphasis> expands to the process id of the shell innovated to run the script. Useful for creating unique temporary filenames relative to this instantiation of the script.
               </para>
             </listitem>
           </itemizedlist>

           <note>
             <para>
               The commandline arguments will be referred to as parameters from now on, this is because <acronym>SH</acronym> also allows the definition of functions which can take parameters and when called the <emphasis>$n</emphasis> family will be redefined, hence these variables are always parameters, its just that in the case of the parent script the parameters are passed via the command line.  One exception is <emphasis>$0</emphasis> which is always set to the name of the parent script regardless of whether it is inside a function or not.
             </para>
           </note>
         </sect4>

         <sect4 id="UnixScripting-Variables-CommandSubstitution"><title>Command Substitution</title>
           <para>
             In the words of the <acronym>SH</acronym> manual <quote>Command substitution allows the output of a command to be substituted in place of the command name itself</quote>.  There are two ways this can be done. The first is to enclose the command like this:
           </para>

           <programlisting>
$(command)
           </programlisting>

           <para>The second is to enclose the command in back quotes like this:</para>

           <programlisting>
`command`
           </programlisting>

           <para>
             The command will be executed in a sub-shell environment and the standard output of the shell will replace the command substitution when the command completes.
           </para>
         </sect4>

         <sect4 id="UnixScripting-Variables-Arithmetic-Expansion"><title>Arithmetic Expansion</title>
           <para>
             Arithmetic expansion is also allowed and comes in the form:
           </para>

           <programlisting>
$((expression))
           </programlisting>

           <para>
             The value of the expression will replace the substitution. Eg:
           </para>

           <programlisting>
!#/bin/sh
echo $((1 + 3 + 4))
           </programlisting>

           <para>Will echo &quot;8&quot; to stdout</para>
         </sect4>
      </sect3>

        <sect3 id="UnixScripting-Control-Constructs"><title>Control Constructs</title>
        <para>
          The flow of control within <acronym>SH</acronym> scripts is done via four main constructs; <emphasis role="strong">if...then...elif..else</emphasis>, <emphasis role="strong">do...while</emphasis>, <emphasis role="strong">for</emphasis> and <emphasis role="strong">case</emphasis>.
        </para>

        <sect4 id="UnixScripting-Control-Constructs-IfThenElse"><title>If..Then..Elif..Else</title>
          <para>
            This construct takes the following generic form, The parts enclosed within ([) and (]) are optional:
          </para>

          <programlisting>
<emphasis role="strong">if</emphasis> <emphasis>list</emphasis>
<emphasis role="strong">then</emphasis> <emphasis>list</emphasis> 
[<emphasis role="strong">elif</emphasis> <emphasis>list</emphasis>
<emphasis role="strong">then</emphasis> <emphasis>list</emphasis>] ...
[<emphasis role="strong">else</emphasis> <emphasis>list</emphasis>]
fi
          </programlisting> 

          <para>
            When a Unix command exits it exits with what is known as an <emphasis>exit status</emphasis>, this indicates to anyone who wants to know the degree of success the command had in performing whatever task it was supposed to do, usually when a command executes without error it terminates with an exit status of zero.  An exit status of some other value would indicate that some error had occurred, the details of which would be specific to the command. The commands' manual pages detail the exit status messages that they produce.
          </para>

          <para>
            A list is defined in the <acronym>SH</acronym> as &quot;a sequence of zero or more commands separated by newlines, semicolons, or ampersands, and optionally terminated by one of these three characters.&quot;, hence in the generic definition of the <emphasis>if</emphasis> above the list will determine which of the execution paths the script takes. For example, there is a command called <command>test</command> on Unix which evaluates an expression and if it evaluates to true will return zero and will return one otherwise, this is how we can test conditions in the <emphasis>list</emphasis> part(s) of the <emphasis>if</emphasis> construct because <command>test</command> is a command.
          </para>

          <para>
            We do not actually have to type the <command>test</command> command directly into the <emphasis>list</emphasis> to use it, it can be implied by encasing the test case within ([) and (]) characters, as illustrated by the following (silly) example:
          </para>

          <programlisting>
#!/bin/sh
if [ &quot;$1&quot; = &quot;1&quot; ]
then
   echo &quot;The first choice is nice&quot;
elif [ &quot;$1&quot; = &quot;2&quot; ]
then
   echo &quot;The second choice is just as nice&quot;
elif [ &quot;$1&quot; = &quot;3&quot; ]
then
   echo &quot;The third choice is excellent&quot;
else
   echo &quot;I see you were wise enough not to choose&quot;
   echo &quot;You win&quot;
fi
          </programlisting>

          <para>
            What this example does is compare the first parameter (command line argument in this case) with the strings &quot;1&quot;, &quot;2&quot; and &quot;3&quot; using <command>test</command>s' (=) test which compares two strings for equality, if any of them match it prints out the corresponding message. If none of them match it prints out the final case. OK the example is silly and actually flawed (the user still wins even if they type in (4) or something) but it illustrates how the <emphasis>if</emphasis> statement works.
          </para>

          <para>
            Notice that there are spaces between (if) and ([), ([) and the test and the test and (]), these spaces must be present otherwise the shell will complain. There must also be spaces between the operator and operands of the test otherwise it will not work properly. Notice how it starts with (if) and ends with (fi), also, notice how (then) is on a separate line to the test above it and that (else) does not require a (then) statement. You must construct this construct exactly like this for it to work properly.
          </para>

          <para>
            It is also possible to integrate logical AND and OR into the testing, by using two tests separated by either &quot;&amp;&amp;&quot; or &quot;||&quot; respectively.  For example we could replace the third test case in the example above with:
          </para>

          <programlisting>
elif [ &quot;$1&quot; = &quot;3&quot;] || [ &quot;$1&quot; = &quot;4&quot; ]
then echo &quot;The third choi...
          </programlisting>

          <para>
            The script would print out &quot;The third choice is excellent&quot; if the first parameter was either &quot;3&quot; <emphasis role="strong">OR</emphasis> &quot;4&quot;. To illustrate the use of &quot;&amp;&amp;&quot; we could replace the third test case with:
          </para>

          <programlisting>
elif [ &quot;$1&quot; = &quot;3&quot;] || [ &quot;$2&quot; = &quot;4&quot; ]
then echo &quot;The third choi...
          </programlisting>

          <para>
            The script would print out &quot;The third choice is excellent&quot; if and only if the first parameter was &quot;3&quot; <emphasis role="strong">AND</emphasis> the second parameter was &quot;4&quot;.
          </para>

          <para>
            &quot;&amp;&amp;&quot; and &quot;||&quot; are both lazily evaluating which means that in the case of &quot;&amp;&amp;&quot;, if the first test fails it wont bother evaluating the second because the list will only be true if they <emphasis>BOTH</emphasis> pass and since one has already failed there is no point wasting time evaluating the second.  In the case of &quot;||&quot; if the first test passes it wont bother evaluating the second test because we only need <emphasis>ONE</emphasis> of the tests to pass for the whole list to pass. See the <command>test</command> manual page for the list of tests possible (other than the string equality test mentioned here). 
          </para>
        </sect4>

        <sect4 id="UnixScripting-Control-Constructs-DoWhile"><title>Do...While</title>
          <para>The <emphasis>Do...While</emphasis> takes the following generic form:</para>

          <programlisting>
<emphasis role="strong">while</emphasis> <emphasis>list</emphasis>            
<emphasis role="strong">do</emphasis> <emphasis>list</emphasis>            
<emphasis role="strong">done</emphasis>            
          </programlisting>

          <para>
            In the words of the <acronym>SH</acronym> manual &quot;The two lists are executed repeatedly while the exit status of the first list is zero.&quot; there is a variation on this that uses <emphasis role="strong">until</emphasis> in place of  <emphasis role="strong">while</emphasis> which executes <emphasis>until</emphasis> the exit status of the first list is zero.  Here is an example use of the  <emphasis role="strong">while</emphasis> statement:
          </para>

          <programlisting>
#!/bin/sh
count=$1                                   # Initialise count to first parameter 
while [ $count -gt 0 ]                     # while count is greater than 10 do
do
   echo $count seconds till supper time!
   count=$(expr $count -1)                 # decrement count by 1
   sleep 1                                 # sleep for a second using the Unix <command>sleep</command> command
done
echo Supper time!!, YEAH!!                 # were finished
          </programlisting>

          <para>
            If called from the commandline with an argument of 4 this script will output
          </para>

          <screen>
4 seconds till supper time!
3 seconds till supper time!
2 seconds till supper time!
1 seconds till supper time!
Supper time!!, YEAH!!
          </screen>

          <para>
            You can see that this time we have used the <option>-gt</option> of the <command>test</command> command implicitly called via '[' and ']', which stands for greater than. Pay careful attention to the formatting and spacing.
          </para>
        </sect4>

        <sect4 id="UnixScripting-Control-Constructs-For"><title>For</title>
          <para>
            The syntax of the for command is:
          </para>

          <programlisting>
            <emphasis role="strong">for</emphasis> <emphasis>variable</emphasis> <emphasis role="strong">in</emphasis> <emphasis>word</emphasis> ...
            <emphasis role="strong">do</emphasis> <emphasis>list</emphasis>
            <emphasis role="strong">done</emphasis>
          </programlisting>

          <para>
            The <acronym>SH</acronym> manual states <quote>The words are expanded, and then the list is executed repeatedly with the variable set to each word in turn.</quote>.  A word is essentially some other variable that contains a list of values of some sort, the <emphasis>for</emphasis> construct assigns each of the values in the <emphasis role="strong">word</emphasis> to <emphasis role="strong">variable</emphasis> and then <emphasis role="strong">variable</emphasis> can be used within the body of the construct, upon completion of the body <emphasis role="strong">variable</emphasis> will be assigned the next value in <emphasis role="strong">word</emphasis> until there are no more values in <emphasis role="strong">word</emphasis>. An example should make this clearer:
          </para>

          <programlisting>
#!/bin/sh
fruitlist=&quot;Apple Pear Tomato Peach Grape&quot;
for fruit in $fruitlist
do
   if [ &quot;$fruit&quot; = &quot;Tomato&quot; ] || [ &quot;$fruit&quot; = &quot;Peach&quot; ]
   then
      echo &quot;I like ${fruit}es&quot;
   else 
      echo &quot;I like ${fruit}s&quot;
   fi
done
          </programlisting>

          <para>
            In this example, <emphasis>fruitlist</emphasis> is <emphasis role="strong">word</emphasis>, <emphasis>fruit</emphasis> is <emphasis role="strong">variable</emphasis> and the body of the statement outputs how much this person loves various fruits but includes an <emphasis>if...then..else</emphasis> statement to deal with the correct addition of letters to describe the plural version of the fruit, notice that the variable <emphasis>fruit</emphasis> was expressed like <emphasis>${fruit}</emphasis> because otherwise the shell would have interpreted the preceding letter(s) as being part of the variable and echoed nothing because we have not defined the variables <emphasis>fruits</emphasis> and <emphasis>fruites</emphasis>  When executed this script will output:
          </para>

          <screen>
I like Apples
I like Pears
I like Tomatoes 
I like Peachs
I like Grapes
          </screen>

          <para>
            Within the <emphasis>for</emphasis> construct, <emphasis role="strong">do</emphasis> and <emphasis role="strong">done</emphasis> may be replaced by <emphasis role="strong">'{'</emphasis> and <emphasis role="strong">'}'</emphasis>. This is not allowed for <emphasis role="strong">while</emphasis>.
          </para>
        </sect4>

        <sect4 id="UnixScripting-Control-Constructs-Case"><title>Case</title>
          <para>
            The case construct has the following syntax:
          </para>

          <programlisting>
<emphasis role="strong">case</emphasis> <emphasis>word</emphasis> <emphasis role="strong">in</emphasis>
pattern) list ;;
...

<emphasis role="strong">esac</emphasis>
          </programlisting>

          <para>
            An example of this should make things clearer:
          </para>

          <programlisting>
!#/bin/sh
case $1
in
1) echo 'First Choice';;
2) echo 'Second Choice';;
*) echo 'Other Choice';;
esac
          </programlisting>

          <para>
            &quot;1&quot;, &quot;2&quot; and &quot;*&quot; are patterns, <emphasis role="strong">word</emphasis> is compared to each pattern and if a match is found the body of the corresponding pattern is executed, we have used &quot;*&quot; to represent everything, since this is checked last we will still catch &quot;1&quot; and &quot;2&quot; because they are checked first.  In our example <emphasis role="strong">word</emphasis> is &quot;$1&quot;, the first parameter, hence if the script is ran with the argument &quot;1&quot; it will output &quot;First Choice&quot;, &quot;2&quot; &quot;Second Choice&quot; and anything else &quot;Other Choice&quot;. In this example we compared against numbers (essentially still a string comparison however) but the pattern can be more complex, see the <acronym>SH</acronym> man page for more information.
          </para>
        </sect4>
      </sect3>

      <sect3 id="UnixScripting-Functions"><title>Functions</title> 
        <para>
          The syntax of an <acronym>SH</acronym> function is defined as follows: 
        </para>

        <programlisting>
name ( ) command
        </programlisting>

        <para>
          It is usually laid out like this:
        </para>

        <programlisting>
name() {
commands
}
        </programlisting>

        <para>
          A function will return with a default exit status of zero, one can return different exit status' by using the notation <emphasis role="strong">return</emphasis> <emphasis>exit status</emphasis>.  Variables can be defined locally within a function using <emphasis role="strong">local</emphasis> <emphasis>name</emphasis>=<emphasis>value</emphasis>.  The example below shows the use of a user defined increment function:
        </para>

        <example id="UnixScripting-Functions-IncrementExample"><title>Increment Function Example</title>
          <programlisting>
#!/bin/sh
inc() { <co id="UnixScripting-Functions-IncrementExample-Function"/>                          # The increment is defined first so we can use it
   echo $(($1 + $2))              # We echo the result of the first parameter plus the second parameter
}

                                  # We check to see that all the command line arguments are present
if [ &quot;$1&quot; &quot;&quot; ] || [ &quot;$2&quot; = &quot;&quot; ] || [ &quot;$3&quot; = &quot;&quot; ]
then
   echo USAGE:
   echo &quot;   counter startvalue incrementvalue endvalue&quot;
else
   count=$1                       # Rename are variables with clearer names 
   value=$2
   end=$3
   while [ $count -lt $end ]      # Loop while count is less than end
   do
      echo $count
      count=$(inc $count $value)  <co id="UnixScripting-Functions-IncrementExample-Call"/># Call increment with count and value as parameters
   done                           # so that count is incremented by value
fi
          </programlisting>
        </example>

        <calloutlist>
          <callout arearefs="UnixScripting-Functions-IncrementExample-Function">
            <para/>
            <programlisting>
inc() { 
   echo $(($1 + $2))
}            
            </programlisting>

            <para>
          The function is defined and opened with <emphasis role="strong">inc() {</emphasis>, the line  <emphasis role="strong">echo $(($1 + $2))</emphasis> uses the notation for arithmetic expression substitution which is  <emphasis role="strong">$((<emphasis>expression</emphasis>))</emphasis> to enclose the expression,  <emphasis role="strong">$1 + $2</emphasis> which adds the first and second parameters passed to the function together, the echo bit at the start echoes them to standard output, we can catch this value by assigning the function call to a variable, as is illustrated by the function call.
            </para>
          </callout>

          <callout arearefs="UnixScripting-Functions-IncrementExample-Call">
            <para/>
            <programlisting>
count=$(inc $count $value)
            </programlisting>
            <para>
              We use command substitution which substitutes the value of a command to substitute the value of the function call whereupon it is assigned to the <emphasis>count</emphasis> variable.  The command within the command substitution block is <emphasis role="strong">inc $count $value</emphasis>, the last two values being its parameters. Which are then referenced from within the function using <emphasis role="strong">$1</emphasis> and <emphasis role="strong">$2</emphasis>. We could have used the other command substitution notation to call the function if we had wanted:
            </para>
            <programlisting>
count=`inc $count $value`
            </programlisting>
          </callout>
        </calloutlist>

        <para>We will show another quick example to illustrate the scope of variables:</para>

        <example id="UnixScripting-Functions-ScopeExample"><title>Variable Scope, Example</title>
          <programlisting>
#!/bin/sh
inc() {
   local value=4  <co id="UnixScripting-Functions-ScopeExample-Newline"/>
   echo &quot;value is $value within the function\\n&quot;
   echo &quot;\\b\$1 is $1 within the function&quot;
}

value=5
echo value is $value before the function
echo &quot;\$1 is $1 before the function&quot;
echo
echo -e $(inc $value) <co id="UnixScripting-Functions-ScopeExample-Call"/>
echo
echo value is $value after the function
echo &quot;\$1 is $1 after the function&quot;
          </programlisting>
        </example>

        <calloutlist>
          <callout arearefs="UnixScripting-Functions-ScopeExample-Newline">
            <para/>
            <programlisting>
inc() {
   local value=4
   echo &quot;value is $value within the function\\n&quot;
   echo &quot;\\b\$1 is $1 within the function&quot;
}
            </programlisting>
            <para>
              We assign a local value to the variable <emphasis>value</emphasis> of 4.  The next three lines construct the the output we would like, remember that this is being echoed to some buffer and will be replace the function call with all the stuff that was passed to stdout within the function when the function exits. So the calling code will be replaced with whatever we direct to standard output within the function. The function is called like this: 
            </para>

            <programlisting>
echo -e $(inc $value)
            </programlisting>

            <para>
              We have passed the option <option>-e</option> to the <command>echo</command> command which causes it to process C-style backslash escape characters, so we can process any backslash escape characters which the string generated by the function call contains. 
            </para>

            <para>
              If we just echo the lines we want to be returned by the function it will not pass the newline character onto the buffer even if we explicitly include it with an escape character reference so what we do is actually include the sequence of characters that will produce a new line within the string so that when it is echoed by the calling code with the <option>-e</option> the escape characters will be processed and the newlines will be placed where we want them. 
            </para>

            <programlisting>
echo &quot;value is $value within the function\\n&quot;
            </programlisting>

            <para>
              Notice how the newline has been inserted with <emphasis role="strong">\\n</emphasis>, the first two backslashes indicate that we want to echo a backslash because within double quotes a backslash indicates to process the next character literally, we have to do this because we are only between double quotes and not the literal-text single quotes. If we had used single quotes we would had have to echo the bit with the newline in separately from the bit that contains <emphasis role="strong">$value</emphasis> otherwise <emphasis role="strong">$value</emphasis> would not be expanded.
            </para>

            <programlisting>
echo &quot;\\b\$1 is $1 within the function&quot;              
            </programlisting>

            <para>
              This is our second line, and is contained within double quotes so that the variable <emphasis>$1</emphasis> will be expanded, <emphasis role="strong">\\b</emphasis> is included so that <emphasis role="strong">\b</emphasis> will be placed in the echoed line and our calling code will process this as a backspace character.  We have to do this because for some reason the shell prefixes a space to the second line if we do not, the backspace removes this space.
            </para>

            <para>The output from this script called with 2 as the first argument is:</para>

            <screen>
value is 5 before the function
$1 is 2 before the function

value is 4 within the function
$1 is 5 within the function

value is 5 after the function
$1 is 2 after the function
            </screen>
          </callout>
        </calloutlist>

        <tip>
          <para>
             You can use &quot;. DIRECTORY/common.sh&quot; to import functions from a script called common.sh in DIRECTORY, a quick example is shown below, first is test.sh:
          </para>
          <programlisting>#!/bin/sh
. ./common.sh
if [ &quot;$1&quot; = &quot;&quot; ]; then
   echo USAGE:
   echo &quot;sh test.sh type&quot;
   exit
fi

if `validtype $1`; then
   echo Valid type
else
   echo Invalid type
fi</programlisting>
          <para>Here is common.sh:</para>

          <programlisting>#!/bin/sh
validtype() {
   if [ &quot;$1&quot; = &quot;TYPEA&quot; ] ||
      [ &quot;$1&quot; = &quot;TYPEB&quot; ] ||
      [ &quot;$1&quot; = &quot;TYPEC&quot; ] ||
      [ &quot;$1&quot; = &quot;TYPED&quot; ] ||
      [ &quot;$1&quot; = &quot;TYPEE&quot; ];
   then
      exit 0
   else
      exit 1 
   fi
}</programlisting>
          <para>
             If you need to learn more, checkout <ulink url="http://www.japarker.btinternet.co.uk/sh/">http://www.japarker.btinternet.co.uk/sh/</ulink> for what looks like an excellent Bourne Shell Programming tutorial.
          </para>
        </tip>
      </sect3>
    </sect2>
  </sect1>
</article>

