In part 3, let's begin looking at how shell scripts can be made even more useful using tests, and looping
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:
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"
echo "hostconfig file exists"
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.
Testing strings might be the second most common use of if-then statements. An example:
if test -n "$abc"
echo "abc is not null"
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.
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 ]]
echo Usage: myscript argument >&2
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.
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.
What if the test being executed needs to occur more than once? Performing repetitious tasks is where looping can be used.
The generic form of a for loop uses the following syntax:
for argument in list
Let's use an example which might be very useful.
for file in /etc
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.
The generic form of a while loop is:
A real example:
while [[ "$var" != "stop" ]]
echo "Input variable (stop to exit) "
echo "variable #1 = $var"
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.