The basic principle of if-then statements is that if a certain condition is met, then one thing happens; if the condition is not met, then another thing happens. That is, if you walk into your office in the morning and you see your daily to-do list, then you sit down and work. If you walk into your office in the morning and you don't see your to-do list, then you get to lounge all day. Or something like that.
As Figure 10.6 shows, you can create if-then statements using if, then, and else commands. When you set up these conditional statements, the computer then has to test the condition to determine whether it's true or false, and act accordingly. In the next example, we set up a fairly simple if-then conditional statement requiring the computer to test whether or not a file exists and tell us what it finds. Use the following steps to get started with if-then statements, and see the More on If-Then sidebar in this section to learn how to expand your if-then statements.
Figure 10.6. Using if-then conditional statements, you can let the computer determine if something is true or not, and then act accordingly.
To Write an If-Then Conditional Statement:
To begin, access your editor and the script file. Here we're adapting an existing script (for feedback) in vi.
if [ -f feedback ]
Start the loop with if, and then follow it with a conditional statement, in this case if [ -f feedback ], which checks for the existence of a file named feedback in the current directory. If that file exists, then the expression is true. See the whole script in Figure 10.6.
More on If-Then
Using the steps provided in this section, try some of these other if-then possibilities:
[ -f filename ] checks to see whether a file exists.
[ ! -a filename ] checks to see whether a file does not exist. The ! symbol (not) makes this test report "true" when the previous example would be "false."
[ -d name ] checks to see whether name exists and is a directory.
[ first -nt second ] checks to see whether the modification date of the first file or directory is newer than the second.
[ first -ot second ] checks to see whether the modification date of the first file or directory is older than the second.
[ -n string ] checks to see whether the string has a length greater than 0.
[ -z string ] checks to see whether the string is null or 0 length.
[ string1 = string2] checks to see whether the first string is equal to the second.
[ string1 != string2] checks to see whether the first string is not equal to the second.
[ \( condition1 \) -a \( condition2 \) ] checks to see whether both conditions are true (conditions can include other conditions).
[ \( condition1 \) -o \( condition2 \) ] checks to see if either condition1 or condition2 is true.
Type man test for more information about creating conditional statements.
then echo "There's feedback on the latest project
On the line immediately after the if statement, enter the command to be carried out or message to be displayed if the if statement is true. In this example, a true if statement would result in "There's feedback on the latest project" being printed onscreen.
else echo "Nope, no feedback yet"
On the next line, use else followed by a statement specifying what should happen if the if statement is false. Here, we specify that "Nope, no feedback yet" would be printed onscreen if the feedback file was not found or had nothing in it.
Immediately after the else statement, announce that you're finished with fi.
Save the script and try it out.
In this example, the script will check to see if feedback exists and print a different message depending on what it finds (Code Listing 10.5).
Code Listing 10.5. The last line produced by the feedback script differs, depending on the files found.
[ejr@hobbes scripting]$ ./deef Greetings! Say, you're looking mighty sharp today! You were most recently working on scripting/. Nope, no feedback yet [ejr@hobbes scripting]$ touch feedback [ejr@hobbes scripting]$ ./deef Greetings! Say, you're looking mighty sharp today! You were most recently working on scripting/. There's feedback on the latest project [ejr@hobbes scripting]$