Free Hosting Online for WorkStations

< Previous | Contents | Next >

Logic

As we discovered in Chapter 27, the (( )) compound command supports a variety of comparison operators. There are a few more that can be used to evaluate logic. Here is the complete list:


Table 34-6: Comparison Operators


Operator Description

Operator Description

<= Less than or equal to


>= Greater than or equal to


< Less than


> Greater than



== Equal to


!= Not equal to


&& Logical AND


|| Logical OR


expr1?expr2:expr3 Comparison (ternary) operator. If expression expr1

evaluates to be non-zero (arithmetic true) then expr2, else expr3.



When used for logical operations, expressions follow the rules of arithmetic logic; that is, expressions that evaluate as zero are considered false, while non-zero expressions are considered true. The (( )) compound command maps the results into the shell’s normal exit codes:



[me@linuxbox ~]$ if ((1)); then echo "true"; else echo "false"; fi

true

[me@linuxbox ~]$ if ((0)); then echo "true"; else echo "false"; fi

false

[me@linuxbox ~]$ if ((1)); then echo "true"; else echo "false"; fi

true

[me@linuxbox ~]$ if ((0)); then echo "true"; else echo "false"; fi

false


The strangest of the logical operators is the ternary operator. This operator (which is modeled after the one in the C programming language) performs a standalone logical test. It can be used as a kind of if/then/else statement. It acts on three arithmetic expressions (strings won’t work), and if the first expression is true (or non-zero) the second expres- sion is performed. Otherwise, the third expression is performed. We can try this on the command line:



[me@linuxbox ~]$ a=0 [me@linuxbox ~]$ ((a<1?++a:--a)) [me@linuxbox ~]$ echo $a

1

[me@linuxbox ~]$ ((a<1?++a:--a))

[me@linuxbox ~]$ echo $a

0

[me@linuxbox ~]$ a=0 [me@linuxbox ~]$ ((a<1?++a:--a)) [me@linuxbox ~]$ echo $a

1

[me@linuxbox ~]$ ((a<1?++a:--a))

[me@linuxbox ~]$ echo $a

0


Here we see a ternary operator in action. This example implements a toggle. Each time the operator is performed, the value of the variable a switches from zero to one or vice versa.

Please note that performing assignment within the expressions is not straightforward.


When attempted, bash will declare an error:


[me@linuxbox ~]$ a=0

[me@linuxbox ~]$ ((a<1?a+=1:a-=1))

bash: ((: a<1?a+=1:a-=1: attempted assignment to non-variable (error token is "-=1")

[me@linuxbox ~]$ a=0

[me@linuxbox ~]$ ((a<1?a+=1:a-=1))

bash: ((: a<1?a+=1:a-=1: attempted assignment to non-variable (error token is "-=1")


This problem can be mitigated by surrounding the assignment expression with parenthe- ses:



[me@linuxbox ~]$ ((a<1?(a+=1):(a-=1)))

[me@linuxbox ~]$ ((a<1?(a+=1):(a-=1)))


Next, we see a more complete example of using arithmetic operators in a script that pro- duces a simple table of numbers:



#!/bin/bash

# arith-loop: script to demonstrate arithmetic operators finished=0

a=0

printf "a\ta**2\ta**3\n" printf "=\t====\t====\n"


until ((finished)); do b=$((a**2))

c=$((a**3))

printf "%d\t%d\t%d\n" $a $b $c ((a<10?++a:(finished=1)))

done

#!/bin/bash

# arith-loop: script to demonstrate arithmetic operators finished=0

a=0

printf "a\ta**2\ta**3\n" printf "=\t====\t====\n"


until ((finished)); do b=$((a**2))

c=$((a**3))

printf "%d\t%d\t%d\n" $a $b $c ((a<10?++a:(finished=1)))

done


In this script, we implement an until loop based on the value of the finished variable. Initially, the variable is set to zero (arithmetic false) and we continue the loop until it be- comes non-zero. Within the loop, we calculate the square and cube of the counter variable

a. At the end of the loop, the value of the counter variable is evaluated. If it is less than 10 (the maximum number of iterations), it is incremented by one, else the variable fin- ished is given the value of one, making finished arithmetically true, thereby termi- nating the loop. Running the script gives this result:


[me@linuxbox ~]$ arith-loop

[me@linuxbox ~]$ arith-loop


a

= 0

1

2

3

4

5

6

7

8

9

10

a

= 0

1

2

3

4

5

6

7

8

9

10


a**2 a**3

==== ====

a**2 a**3

==== ====

0

1

4

9

16

25

36

49

64

81

100

0

1

4

9

16

25

36

49

64

81

100

0

1

8

27

64

125

216

343

512

729

1000

0

1

8

27

64

125

216

343

512

729

1000

Top OS Cloud Computing at OnWorks: