12.3. Arithmetic
The Korn shell supports both integer and floating-point arithmetic, but floating-point arithmetic is available only on versions of the Korn shell newer than 1988. The typeset command is used for assigning types. See Table 12.3 for the typeset command.
Table 12.3. and ArithmeticCommand | Alias | Meaning |
---|
typeset –i variable | integer variable | variable is only allowed integer assignment | typeset –i# | | # is the base number for the integer | On Versions of ksh Newer Than 1988 | typeset –F variable | | Floating-point number assignment | typeset –E variable | Float variable | Floating-point number assignment |
12.3.1 The Integer Type
Variables can be declared as integers with the typeset –i command or its alias, integer. If you attempt to assign any string value, ksh returns an error. If you assign a floating-point number, the decimal point and the fractional value will be truncated. The integer alias can be used instead of typeset –i. Numbers can also be represented in different bases such as binary, octal, and hex.
Example 12.8.
1 $ typeset –i num or integer num # integer is an alias for typeset -i
2 $ num=hello
/bin/ksh: hello: bad number
3 $ num=5 + 5
/bin/ksh: +: not found
4 $ num=5+5
$ echo $num
10
5 $ num=4*6
$ echo $num
24
6 $ num="4 * 6"
$ echo $num
24
7 $ num=6.789
$ echo $num
6
EXPLANATION
The typeset command with the –i option creates an integer variable, num. Trying to assign the string hello to the integer variable num causes an error. The whitespace is removed and arithmetic is performed. Multiplication is performed and the result assigned to num. The whitespace is quoted so that the multiplication can be performed and to keep the shell from expanding the wildcard (*). Since the variable is set to integer, the fractional part of the number is truncated.
12.3.2 Using Different Bases
Numbers can be represented in decimal (base 10), octal (base 8), and so forth, by using the typeset command and with the –i option and the base number.
Example 12.9.
1 $ num=15
2 $ typeset –i2 num # binary
$ print $num
2#1111
3 $ typeset –i8 num # octal
$ print $num
8#17
4 $ typeset –i16 num # hex
$ print $num
16#f
5 $ read number
2#1101
$ print $number
2#1101
6 $ typeset –i number
$ print $number
2#1101
7 $ typeset –i10 number # decimal
$ print $number
13
8 $ typeset –i8 number # octal
$ print $number
8#15
EXPLANATION
The variable num is assigned the value 15. The typeset command converts the number to a binary format. The display is the base of the number (2), followed by a pound sign (#), and the value of the number in binary. The typeset command converts the number to an octal format and displays the value of the number in base 8. The typeset command converts the number to hexadecimal format and displays the value of the number in base 16. The read command accepts input from the user. The input is entered in binary format, stored in the variable number, and displayed in binary format. The typeset command converts number to an integer. It still displays in binary format. The typeset command converts number to a decimal integer and displays it. The typeset command converts number to octal and displays its value in base 8.
12.3.3 Listing Integers
The typeset command with only the –i argument will list all preset integers and their values, as shown in the following display.
$ typeset –i
ERRNO=2
LINENO=1
MAILCHECK=600
OPTIND=1
PPID=4881
RANDOM=25022
SECONDS=47366
TMOUT=0
n=5
number=#15
12.3.4 Arithmetic Operators and the let Command
The let command is a Korn shell built-in command that is used to perform integer arithmetic. (See Table 12.4.) This replaces the Bourne shell method of integer testing. The alternative and preferred way to use the let command is with the (( )) operator.
Example 12.10.
1 $ i=5
2 $ let i=i+1
$ print $i
6
3 $ let "i = i + 2"
$ print $i
8
4 $ let "i+=1"
$ print $i
9
Table 12.4. let OperatorsOperator | Meaning |
---|
– | Unary minus | ! | Logical NOT | ~ | Bitwise NOT | * | Multiply | / | Divide | % | Remainder | + | Add | – | Subtract | << | Bitwise left shift | >> | Bitwise right shift | <= >= < > == != | Comparison operators | & | Bitwise AND | ^| | Exclusive OR | && | Logical AND | || | Logical OR | ! | Unary NOT | = | Assignment | *= /= %= += –= <<= >>= &= ^= |= | Shortcut assignments |
EXPLANATION
The variable i is assigned the value 5. The let command will add 1 to the value of i. The $ (dollar sign) is not required for variable substitution when performing arithmetic. The quotes are needed if the arguments contain whitespace. The shortcut operator += is used to add 1 to the value of i.
Example 12.11.
(The Command Line)
1 $ (( i = 9 ))
2 $ (( i = i * 6 ))
$ print $i
54
3 $ (( i > 0 && i <= 10 ))
4 $ print $?
1
$ j=100
5 $ (( i < j || i == 5 ))
6 $ print $?
0
7 $ if (( i < j && i == 54 ))
> then
> print True
>fi
True
$
EXPLANATION
The variable i is assigned the value 9. The (( )) operators are an alternate form of the let command. Because the expression is enclosed in double parentheses, spaces are allowed between the operators. The variable i is assigned the product of i*6. The numeric expressions are tested. If both expressions are true, 0 exit status is returned. The special ? variable holds the exit status of the last command (the let command) executed. Because the value is 1, the command failed (evaluated as false). The numeric expressions are tested. If one of the expressions is true, 0 exit status is returned. The special ? variable holds the exit status of the last command (the let command) executed. Because the value is 0, the command succeeded (evaluated as true). The if conditional command precedes the let command. The secondary prompt appears while waiting for the command to be completed. If the exit status is 0, the commands after the then statement are executed; otherwise, the primary prompt returns.
|