Previous Section  < Day Day Up >  Next Section

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 Arithmetic

Command

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

  1. The typeset command with the –i option creates an integer variable, num.

  2. Trying to assign the string hello to the integer variable num causes an error.

  3. The whitespace must be quoted or removed unless the (( )) operators are used (see "Arithmetic Operators and the let Command" on page 662).

  4. The whitespace is removed and arithmetic is performed.

  5. Multiplication is performed and the result assigned to num.

  6. The whitespace is quoted so that the multiplication can be performed and to keep the shell from expanding the wildcard (*).

  7. 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.[1]

[1] Bases greater than 36 are available on versions of the Korn shell that are newer than 1988.

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

  1. The variable num is assigned the value 15.

  2. 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.

  3. The typeset command converts the number to an octal format and displays the value of the number in base 8.

  4. The typeset command converts the number to hexadecimal format and displays the value of the number in base 16.

  5. 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.

  6. The typeset command converts number to an integer. It still displays in binary format.

  7. The typeset command converts number to a decimal integer and displays it.

  8. 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 Operators[a]

Operator

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


[a] The ++ and –– operators are supported on versions of ksh that are newer than 1988

EXPLANATION

  1. The variable i is assigned the value 5.

  2. The let command will add 1 to the value of i. The $ (dollar sign) is not required for variable substitution when performing arithmetic.

  3. The quotes are needed if the arguments contain whitespace.

  4. 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

  1. 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.

  2. The variable i is assigned the product of i*6.

  3. The numeric expressions are tested. If both expressions are true, 0 exit status is returned.

  4. 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).

  5. The numeric expressions are tested. If one of the expressions is true, 0 exit status is returned.

  6. 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).

  7. 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.

    Previous Section  < Day Day Up >  Next Section