| Previous | Table of Contents | Next |
|
|
||
| C Shell | ||
|---|---|---|
| The C shell supports additional operators and evaluates operators in a different order. The operators are as follows. | ||
| ( ) | < > <= >= | |
| ~ | == != =~ !~ | |
| ! | & | |
| * / % | ^ | |
| + - | | | |
| * / % | && | |
| << >> | || | |
| The operators == and != are the same as ksh. The third form, =~, checks for the existence of the left string in the right string. The right string may contain filename expansion characters. The !~ operator is the opposite of =~. | ||
|
|
||
FURTHER DISCUSSION
The let command can perform assignments and comparisons. Assignments allow you to assign a numeric value to a variable, whereas comparison compares the values. You can use the return code from comparison type commands to control conditional shell programming statements. For example,
cj> lcv=1
cj> while (( lcv <= 20 ))
do
echo Iteration number ${lcv}
let "lcv = lcv + 1"
done
displays the number of times a loop has been performed until the lcv variable equals 20.
Double quoted expressions must be separated by a space. For example, the following two lines:
cj> let j=j+10 k=k+1
cj> let "j = j + 10" "k = k + 1"
are considered identical by let.
As described in the Argument section the expression can be composed in various formats. Some of these are discussed in the following examples to clarify their usage. For example,
cj> let "num = ( 10 + 90 ) / 5"
assigns 20 to num, whereas
cj> let "nbr = 10 + 90 / 5"
assigns 28 to nbr. To display the values in the two variables type
cj> echo $num $nbr
20 28
One of the more confusing uses of the let command is the base#number notation for handling different base number systems. Once you use a base, the variable you used is tagged with the base attribute you gave it with the base#number notation. For instance, if you use
cj> (( N = 2#1011 )) # assign binary 1011 ( decimal 11 ) to var N
then N is base 2 (typeset -i2 N). If you want to assign base 2 numbers to N, they must be either base 2 variables or you must precede the number with 2#. To add another binary number to N you could type
cj> (( N = N + 2#10000000 )) # add binary 10000000 ( decimal 128 ) to N
To display the result, use the echo command:
cj> echo $N
10001011
Once the base has been changed, the variable will always be displayed in that base. You can intermix the bases; for example:
cj> (( N = 100 ))
assigns decimal 100 to N. When you display N, it is in binary:
cj> echo $N
1100100
RELATED COMMANDS
Refer to the expr command described in Module 48 and the typeset command described in Module 145.
RETURN CODES
The let command returns a zero (true) exit status if the value of the expression is nonzero. If the value of the expression is zero, the exit status is 1. If multiple expressions are provided on the command line, then let determines its return code based on the last expression.
APPLICATIONS
The let command is a replacement for the old method of performing shell arithmetic using the expr command. It can be used for general purpose integer math. It performs arithmetic operations 10 to 30 times faster than using the expr command. It is commonly used inside looping structures to increment the loop control variable. It may also prove useful when working with arrays. You can perform indexing into your arrays with numbers calculated by let, again speeding up your shell script.
TYPICAL OPERATION
In this activity you use the let command to control a loop to print the numbers from 1 to 100 on your screen. Using let to increment the count to control a loop is one of the most common uses of the command. Begin at the shell prompt.
cj> i=0 cj> while (( ( i = i + 1 ) <= 100 )) do echo $i done
|
|
||
| Bourne Shell | ||
|---|---|---|
| cj> i=0 | ||
| cj> while expr $i \<= 100 > /dev/null | ||
| do | ||
| i=`expr $i+1` | ||
| echo $i | ||
| done | ||
|
|
||
| C Shell | |
|---|---|
| cj> set x=0 | |
| cj> while ( $x <= 100 ) | |
| echo $x | |
| @ $x=$x + 1 | |
| end | |
|
|
|
| Previous | Table of Contents | Next |