Section 6.9. Conditional Expressions


[Page 223 (continued)]

6.9. Conditional Expressions

You can also compare values (usually stored in shell variables) with each other and branch to different commands depending on the outcome of the comparison. We'll see in the next section on control structures how you control what you do after the comparison.

6.9.1. Arithmetic Tests

Like arithmetic operations, arithmetic tests are enclosed in double parentheses. The types of comparisons you can do include those in Figure 6-27.

Figure 6-27. Arithmetic conditional operators.

<= >= < >

Less than or equal to, greater than or equal to, less than, greater than comparisons.

== !=

Equal, not equal.

!

Logical NOT.

&&

Logical AND.

||

Logical OR.



[Page 224]

This all makes it simple to do a bit of math in a Bash script. We'll count up to 20 and test to see what numbers divide into 20 evenly (don't worry too much about the while construct just yet, we'll see it a bit later):

$ cat divisors.sh #!/bin/bash # declare -i testval=20 declare -i count=2     # start at 2, 1 always works while (( $count <= $testval )); do   (( result = $testval % $count ))   if (( $result == 0 )); then   # evenly divisible     echo " $testval is evenly divisible by $count"   fi   (( count++ )) done $ bash divisors.sh  20 is evenly divisible by 2  20 is evenly divisible by 4  20 is evenly divisible by 5  20 is evenly divisible by 10  20 is evenly divisible by 20 $ _ 


6.9.2. String Comparisons

String conditional operators are listed in Figure 6-28.

Figure 6-28. String conditional operators.

-n string

True if length of string is non-zero.

-z string

True if length of string is zero.

string1 == string2

True if strings are equal.

string1 != string2

True if strings are not equal.


6.9.3. File-Oriented Expressions

File-oriented conditional operator are listed in Figure 6-29.

Figure 6-29. File-oriented conditional operators.

[Page 225]

-a file

True if the file exists.

-b file

True if the file exists and is a block-oriented special file.

-c file

True if the file exists and is a character-oriented special file.

-d file

True if the file exists and is a directory.

-e file

True if the file exists.

-f file

True if the file exists and is a regular file.

-g file

True if the file exists and its "set group ID" bit is set.

-p file

True if the file exists and is a named pipe.

-r file

True if the file exists and is readable.

-s file

True if the file exists and has a size greater than zero.

-t fd

True if the file descriptor is open and refers to the terminal.

-u file

True if the file exists and its "set user ID" bit is set.

-w file

True if the file is writable.

-x file

True if the file exists and is executable.

-O file

True if the file exists and is owned by the effective user ID of the user.

-G file

True if the file exists and is owned by the effective group ID of the user.

-L file

True if the file exists and is a symbolic link.

-N file

True if the file exists and has been modified since it was last read.

-S file

True if the file exists and is a socket.

file1 nt file2

True if file1 is newer than file2.

file1 ot file2

True if file1 is older than file2.

file1 ef file2

True if file1 and file2 have the same device and inode numbers.



[Page 225]

A simple example of a file operation might be:

$ cat owner.sh #!/bin/bash # if [ -O /etc/passwd ]; then    echo "you are the owner of /etc/passwd." else    echo "you are NOT the owner of /etc/passwd." fi $ bash owner.sh 
[Page 226]
you are NOT the owner of /etc/passwd. $_





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net