Tuesday, March 22, 2011

Conditional Operations

Let's look at the if/then/else construct in a Bash shell script and see how to control the flow of a script with conditional logic. The general form of if/then/else is shown here, with the actual syntax shown in boldface and the parts you must supply in normal type:
if [ condition is true ]

execute these commands


execute those commands


The else clause is optional, but you must end the construct with the fi command. You can also have nested if clauses by using the elif command like this:
if [ condition1 is true ]
execute these commands
elif [
condition2 is true ]
execute these commands
execute those commands

So what kind of conditions can we test for? If you're dealing with numbers, here are the conditional expressions you can use. In other words, any of these expressions can go inside the brackets on the if or elif statement:
num1 -eq num2 True if num1 equals num2.
num1 -ne num2 True if num1 is not equal to num2.
num1 -lt num2 True if num1 is less than num2.
num1 -gt num2 True if num1 is greater than num2.
num1 -le num2 True if num1 is less than or equal to num2.
num1 -ge num2 True if num1 is greater than or equal to num2.
If you're comparing character strings, these are the valid conditional expressions:
str1 = str2 True if str1 and str2 are identical.
str1 != str2 True if str1 and str2 are not identical.
-n str1 True if str1 is not null (length is greater than zero).
-z str1 True if str1 is null (length is zero).
You can also test certain file conditions, such as whether or not files exist, the type of file, and so on. Here are the conditional expressions for files:
-f somefile True if somefile exists and is an ordinary file.
-d somefile True if somefile exists and is a directory.
-s somefile True if somefile contains data (the size is not zero).
-r somefile True if somefile is readable.
-w somefile True if somefile is writable.
-x somefile True if somefile is executable.
And finally, here are the logical operators, for performing tests that involve and, or, and not conditions.
cond1 -a cond2 True if both cond1 and cond2 are true.
cond1 -o cond2 True if either cond1 or cond2 is true.
! cond1 True if cond1 is false.
Some if/then/else Examples
Here are some examples using the conditional expressions just listed. Note that the spaces on either side of the square brackets are not optional!
if [ $carprice -gt 20000 ]

echo 'Too rich for my blood.'


echo 'Can you get that model in blue?'


if [
 $maker = 'Buick' 

echo 'Have you driven a Ford lately?'


if [
 -r $1 -a -s $1 

echo "The $1 file is readable and contains data."


The case Statement
Bash provides a case statement that lets you compare a string with several possible values and execute a block of code when it finds a match. Here's an example of the case command, with the syntax shown in boldface and the parts you would supply in normal type:
case $1 in

In this example, if the value of $1 was -a, the first block of commands would execute. If the value of $1 was -f, the second block of commands would execute. Otherwise, the third block of commands, following the asterisk clause, would execute. (Think of the asterisk as meaning "match anything.")
You can put as many commands as you need in place of commands in the sample, but be sure to end the block with a double semicolon. Only the first matching block of commands will execute in a case statement, and you must signal the end of the construct with the esac command.
Previous Lesson: Shell Script Variables
Next Lesson: Shell Script Looping

Table 13.10: Special Shell Variables Used in Scripts
The number of arguments.
The command name.
$1$2, ... , $9
The individual arguments of the command.
The entire list of arguments, treated as a single word.
The entire list of arguments, treated as a series of words.
The exit status of the previous command. The value 0 denotes successful completion.
The process id of the current process.

Table 13.11: Commonly Used Argument Forms of the test Command
-d file
The specified file exists and is a directory.
-e file
The specified file exists.
-r file
The specified file exists and is readable.
-s file
The specified file exists and has non-zero size.
-w file
The specified file exists and is writable.
-x file
The specified file exists and is executable.
-L file
The specified file exists and is a symbolic link.
f1 -nt f2
File f1 is newer than file f2.
f1 -ot f2
File f1 is older than file f2.
-n s1
String s1 has nonzero length.
-z s1
String s1 has zero length.
s1 = s2
String s1 is the same as string s2.
s1 != s2
String s1 is not the same as string s2.
n1 -eq n2
Integer n1 is equal to integer n2.
n1 -ge n2
Integer n1 is greater than or equal to integer n2.
n1 -gt n2
Integer n1 is greater than integer n2.
n1 -le n2
Integer n1 is less than integer n2.
n1 -lt n2
Integer n1 is less than or equal to integer n2.
n1 -ne n2
Integer n1 is not equal to integer n2.
The not operator, which reverses the value of the following condition.
The and operator, which joins two conditions. Both conditions must be true for the overall result to be true.
The or operator, which joins two conditions. If either condition is true, the overall result is true.
\( ... \)
You can group expressions within the test command by enclosing them within \( and \).

No comments:

Post a Comment