Articles‎ > ‎

Shell scripting 101: Part 3

posted Dec 19, 2008, 5:45 AM by Philip Rinehart   [ updated Dec 19, 2008, 5:46 AM by Greg Neagle ]
In part 3, let's begin looking at how shell scripts can be made even more useful using tests, and looping

Tests

Testing can be quite simple, and doesn't need to be overly complicated, relying on the syntax for if-then to make a decision about performing a particular action.  In its most simple form:

if cmd
then
    secondcmd
fi

 Notice the rather idiosyncratic nature of shell scripting.  If-then commands are terminated with the word if reversed, that is fi.  Most of the testing functionality in shell scripting behaves in a similar manner.  What exactly is happening in any if statement?  The shell is evaluating the if statements exit status.  If it is zero, it will execute the block of code, if not, it does not execute the code.  In bash, it is very common to use an if-then statement to test the existence of a particular file, or comparison of two strings.  

Testing a file

An example first:

if test -e "/etc/hostconfig"
then
echo "hostconfig file exists"
fi

Deconstructing the script, the if-then statement is testing for the existence of the file /etc/hostconfig using the -e option.  If it is it executes the command echo "hostconfig file exists".  

One important note, when executing test functions, it is best practice to quote any variable, so that test receives a single argument.  Not quoting variables in testing can lead to unexpected results.

String comparisons

Testing strings might be the second most common use of if-then statements.  An example:

if test -n "$abc"
then
echo "abc is not null"
fi

Walking through this script, the value of a string is being tested.  In this case, it is being tested for it's non-null value.  Note, this is not testing whether the string has been initialized or not initialized.

Numeric comparisons

The last test that can be used is a test of the number of arguments passed to a shell script.  In practice, this test can be used to inform the user about the proper usage of a simple shell script.   

if [[ $# != 1 ]]
then
echo Usage: myscript argument >&2
exit 1
fi

 Wait a minute, this example is different than all the others? Why?  In this example, an equivalent to using the test operator is using the series of double brackets.  It is also slightly more tolerant of conditional statements.  Returning to the example, note the usage of the special variable $#.  This variable can be used in scripts to test the number of arguments passed to the script.  In this case, the script expects one argument, so the if-then test is testing the number of arguments passed to the script.

Exit values

A quick note about the above example.  In writing any shell script, it is good practice to provide an exit value.  In fact, when using other tools, such as writing a custom login hook, the exit value can be required.  An exit value of 0 is considered a success, any other value is considered a failure.  Again, returning to the example, note that the shell script fails, and passes an exit value of 1 if the correct number of arguments are not used.

Looping

What if the test being executed needs to occur more than once?  Performing repetitious tasks is where looping can be used.

For loops

The generic form of  a for loop uses the following syntax:

for argument in list
do
command1
command2
done

Let's use an example which might be very useful.

for file in /etc
do
ls -l
done

 This example takes advantage of shell scripting to process files in a directory.  Because any directory can be represented as a list, it's very powerful to use this to process any listing, including a supplied list.

While loops

The generic form of a while loop is:

while test
do
cmd
done

 A real example:

while [[ "$var" != "stop" ]]
do
echo "Input variable (stop to exit) "
read var
echo "variable #1 = $var"
echo
done

This example tests the value of a variable read from the commandline.  If it is not the value "stop", it continues to loop.  When the word stop is typed, it completes the loop.

 We've now finished at looking some of the basics of how to use testing, using if-then, for, and while testing methods.  Part 4 of this shell scripting series will look at how to use functions and including the results of commands in shell scripts.

Comments