Arithmetic Operators

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 6.  Performing Arithmetic


The following sections contain examples for each of the arithmetic operators available to the Korn shell. Table 6.1 lists all of the arithmetic operators available to the Korn shell in order of precedence.

?span>expression (Unary Minus)

Evaluates to the negative value of expression.

 $ ((X=?))  $ ((Y= X + 2))  $ print ?"$X $Y"  ?span>7 9 

The print ?/span> command is used so that the negative sign is not interpreted as an argument.

!expression (Logical Negation)

The ! operator returns 0 (true) for expressions that do not evaluate to zero, or 1 (false) if they do.

 $ X=0  $ ((X=!X)); print "$X"  1  $ ((X=!X)); print "$X"  0 

~expression (Bitwise Negation)

Evaluates to the bitwise negated value (one's complement) of expression. It returns a value that contains a 1 in each bit position where expression contains 0, and a 0 in each bit position where expression contains 1.

 $ X=2#011  $ ((X=~X)); print ?"$X"  ?span>2#100 

expression1 * expression2 (Multiplication) expression1 *= expression2 (Multiply Assign)

Evaluates to the product of expression1 multiplied by expression2. The second format assigned the result of the evaluation to expression1.

 $ ((X=5 * 4)); print "$X"  20  $ ((X=3 * 1 * 2 * 4)); print "$X"  24  $ ((X*=2)); print "$X"  48 

expression1 / expression2 (Division) expression1 /= expression2 (Divide Assign)

Evaluates to the quotient of expression1 divided by expression2. The second format assigned the result of the evaluation to expression1.

 $ Y=50  $ ((X=Y / 10)); print "$X"  5  $ ((X=21 / 5)); print "$X"  4  $ ((X/=2)); print "$X"  2 

expression1 % expression2 (Modulo) expression1 %= expression2 (Modulo Assign)

Evaluates to the remainder of expression1 divided by expression2. The second format assigned the result of the evaluation to expression1.

 $ ((X=20 % 7)); print "$X"  6  $ ((X=11 % 4)); print "$X"  3  $ ((X%=2)); print "$X"  1 

expression1 + expression2 (Addition) expression1 += expression2 (Increment)

Evaluates to the sum of expression1 and expression2. The second format assigned the result of the evaluation to expression1.

 $ ((X=1 + 2)); print "$X"  3  $ ((X=4 + 1 + 3)); print "$X"  8  $ ((X+=1)); print "$X"  9 

expression1 ?expression2 (Subtraction) expression1 ? expression2 (Decrement)

Evaluates to the difference of expression1 and expression2. The second format assigned the result of the evaluation to expression1.

 $ ((X=3 ?1)); print "$X"  2  $ ((X=X ?1)); print "$X"  1  $ ((X?1)); print "$X"  0 

identifier = expression (Assignment)

Assigns identifier the value of expression.

 $ ((X=12)); print "$X"  12  $ Y=7  $ ((X=Y)); print "$X"  2 

expression1 << expression2 (Left shift) expression1 <<= expression2 (Left Shift Assign)

Left shift expression1 by the number of bits specified in expression2. The second format assigned the result of the evaluation to expression1.

 $ typeset  i2 X  $ ((X=2#11 << 1)); print "$X"  2#110  $ ((X=2#110 << 2)); print "$X"  2#11000  $ ((X=2#11000 << 3)); print "$X"  2#11000000 

expression1 >> expression2 (Right Shift) expression1 >>= expression2 (Right Shift Assign)

Right shift expression1 by the number of bits from expression2. The second format assigned the result of the evaluation to expression1.

 $ typeset  i2 X  $ ((X=2#10101 >> 2)); print "$X"  2#101  $ ((X=2#101 >> 1)); print "$X"  2#10  $ ((X>>=1)); print "$X"  2#1 

expression1 <= expression2 (Less Than or Equal)

Evaluates to 0 (true) if expression1 is less than or equal to expression2, otherwise evaluates to 1 (false).

 $ ((1 <= 2)) && print "1 is less than 2"  1 is less than 2  $ ((3 <= 2)) || print "3 is not less than 2"  3 is not less than 2 

expression1 < expression2 (Less Than)

Evaluates to 0 (true) if expression1 is less than expression2, otherwise evaluates to 1 (false).

 $ ((1 < 2)); print "$?"  0  $ ((3 < 2)); print "$?"  1 

expression1 >= expression2 (Greater Than Or Equal)

Evaluates to 0 (true) if expression1 is greater than or equal to expression2, otherwise evaluates to 1 (false).

 $ ((3 >= 2)) && print "3 is greater than 2"  3 is greater than 2  $ ((1 >= 2)) || print "1 is not greater than 2"  1 is not greater than 2 

expression1 > expression2 (Greater Than)

Evaluates to 0 (true) if expression1 is greater expression2, otherwise evaluates to 1 (false).

 $ ((3 > 2)); print $?  0  $ ((1 > 2)); print $?  1 

expression1 == expression2 (Equal To)

Evaluates to 0 (true) if expression1 is equal to expression2, otherwise evaluates to 1 (false).

 $ ((3 == 3)) && print "3 is equal to 3"  3 is equal to 3  $ ((4 == 3)) || print "4 is not equal to 3"  4 is not equal to 3 

expression1 != expression2 (Not Equal To)

Evaluates to 0 (true) if expression1 is not equal to expression2, otherwise evaluates to 1 (false).

 $ ((4 != 3)); print "$?"  0  $ ((3 != 3 )); print "$?"  1 

expression1 & expression2 (Bitwise AND) expression1 &= expression2 (Bitwise AND Assign)

Returns a value that contains a 1 in each bit where there is a 1 in both expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.

 $ typeset  i2 X  $ ((X=2#11 & 2#10)); print "$X"  2#10  $ ((X=2#101 & 2#111)); print "$X"  2#101  $ ((X&=2#001)); print "$X"  2#1 

expression1 ^ expression2 (Bitwise XOR) expression1 ^= expression2 (Bitwise XOR Assign)

Returns a value that contains a 1 in each bit where there is a 1 in only one of the expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.

 $ typeset  i2 X  $ ((X=2#11 ^ 2#10)); print "$X"  2#1  $ ((X=2#101 ^ 2#011)); print "$X"  2#110  $ ((X^=2#100)); print "$X"  2#10 

expression1 | expression2 (Bitwise OR) expression1 |= expression2 (Bitwise OR Assign)

Returns a value that contains a 1 in each bit where there is a 1 in either of the expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.

 $ typeset  i2 X  $ ((X=2#11 | 2#10)); print "$X"  2#11  $ ((X=2#101 | 2#011)); print "$X"  2#111  $ ((X|=2#1001)); print "$X"  2#1111 

expression1 && expression2 (Logical AND)

If expression1 evaluates to 0 (true), expression2 is evaluated. The value of the entire expression is 0 (true) only if expression1 and expression2 are both true. Since both X and Y are equal to 1, the entire expression returns 0, and the print command is executed:

 $ X=1 Y=1  $ ((X==1 && Y==1)) && print "X and Y equal 1"  X and Y equal 1 

Now only X is equal to 1, so the entire expression returns 1, and the print command is not executed:

 $ unset Y  $ ((X==1 && Y==1)) && print "X and Y equal 1"  $ 

expression1 || expression2 (Logical OR)

If expression1 evaluates to non-zero (false), expression2 is evaluated. The value of the entire expression is 0 (true) if either expression1 or expression2 are true. Since X is less than 2, the entire expression returns 0, and the print command is executed:

 $ X=1 Y=2  $ ((X<2 || Y<5)) && print "X or Y less than 2"  X or Y less than 2 

Now, neither X nor Y are less than 2, so the entire expression returns 1, and the print command is not executed:

 $ X=2 Y=2  $ ((X<2 || Y<5)) && print "X or Y less than 2"  $ 

(expression) (Override Precedence)

The () operators are used to override precedence rules. In the next expression, normal precedence rules cause the Y * Z operation to be performed first:

 $ X=1 Y=2 Z=3  $ ((TMP=X + Y * Z))  $ print $TMP  7 

If the expression is rewritten using the precedence override operators, the X + Y operation is performed first:

 $ ((TMP=(X + Y) * Z))  $ print $TMP  9 

       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

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