Pages

Monday, May 9, 2016

calculations in bash


Arithmetic Operators Order of Precedence

Operators are evaluated in order of precedence. The levels are listed in order of decreasing precedence (quoting form the bash man page).

       id++ id--
              variable post-increment and post-decrement
       ++id --id
              variable pre-increment and pre-decrement
       - +    unary minus and plus
       ! ~    logical and bitwise negation
       **     exponentiation
       * / %  multiplication, division, remainder
       + -    addition, subtraction
       << >>  left and right bitwise shifts
       <= >= < >
              comparison
       == !=  equality and inequality
       &      bitwise AND
       ^      bitwise exclusive OR
       |      bitwise OR
       &&     logical AND
       ||     logical OR
       expr?expr:expr
              conditional operator
       = *= /= %= += -= <<= >>= &= ^= |=
              assignment
       expr1 , expr2

              comma

Arithmetic calculations using Inbuilt bash options

value1=5;value2=7
addition=$(( value1 + value2 ))
echo "$value1 + $value2 = $addition "

value1=5;value2=7
subtraction=$(( value1 - value2 ))
echo "$value1 - $value2 = $subtraction "
subtraction=$(( value2 - value1 ))
echo "$value2 - $value1 = $subtraction "

value1=5;value2=7
division=$(( value1 / value2 ))
echo "$value1 / $value2 = $division "
division=$(( value2 / value1 ))
echo "$value2 / $value1 = $division "

value1=5;value2=7
multiplication=$(( value1 * value2 ))
echo "$value1 * $value2 = $multiplication "

value1=5;value2=7
modulus=$(( value1 % value2 ))
echo "$value1 % $value2 = $modulus "
modulus=$(( value2 % value1 ))
echo "$value2 % $value1 = $modulus "

value1=5;value2=7
post_increment=$(( value1++ ))
echo "$value1 after post increment = $post_increment "
post_decrement=$(( value2-- ))
echo "$value2 after post decrement = $post_decrement "

value1=5;value2=7
pre_increment=$(( ++value1 ))
echo "$value1 after pre increment = $pre_increment "
pre_decrement=$(( --value2 ))
echo "$value2 after pre decrement = $pre_decrement "

value1=5;value2=7
exponentiation=$(( value1 ** value2 ))
echo "$value1 ** $value2 = $exponentiation "
exponentiation=$(( value2 ** value1 ))

echo "$value2 ** $value1 = $exponentiation "



Arithmetic calculations using let command

value1=5;value2=7
let addition=value1+value2
echo "$value1 + $value2 = $addition "

value1=5;value2=7
let subtraction=value1-value2
echo "$value1 - $value2 = $subtraction "
let subtraction=value2-value1
echo "$value2 - $value1 = $subtraction "

value1=5;value2=7
let division=value1/value2
echo "$value1 / $value2 = $division "
let division=value2/value1
echo "$value2 / $value1 = $division "

value1=5;value2=7
let multiplication=value1*value2
echo "$value1 * $value2 = $multiplication "

value1=5;value2=7
let modulus=value1%value2
echo "$value1 % $value2 = $modulus "
let modulus=value2%value1
echo "$value2 % $value1 = $modulus "

value1=5;value2=7
let post_increment=value1++
echo "$value1 after post increment = $post_increment "
let post_decrement=value2--
echo "$value2 after post decrement = $post_decrement "

value1=5;value2=7
let pre_increment=++value1
echo "$value1 after pre increment = $pre_increment "
let pre_decrement=--value2
echo "$value2 after pre decrement = $pre_decrement "

value1=5;value2=7
let exponentiation=value1**value2
echo "$value1 ** $value2 = $exponentiation "
let exponentiation=value2**value1

echo "$value2 ** $value1 = $exponentiation "

Arithmetic calculations using expr command


value1=5;value2=7
addition=$( expr $value1 + $value2 )
echo "$value1 + $value2 = $addition "

value1=5;value2=7
subtraction=$( expr $value1 - $value2 )
echo "$value1 - $value2 = $subtraction "
subtraction=$( expr $value2 - $value1 )
echo "$value2 - $value1 = $subtraction "

value1=5;value2=7
division=$( expr $value1 / $value2 )
echo "$value1 / $value2 = $division "
division=$( expr $value2 / $value1 )
echo "$value2 / $value1 = $division "

value1=5;value2=7
multiplication=$( expr $value1 \* $value2 )
echo "$value1 * $value2 = $multiplication "

value1=5;value2=7
modulus=$( expr $value1 % $value2 )
echo "$value1 % $value2 = $modulus "
modulus=$( expr $value2 % $value1 )
echo "$value2 % $value1 = $modulus "

Arithmetic calculations using bc command

value1=5;value2=7
addition=$( echo $value1+$value2 |bc )
echo "$value1 + $value2 = $addition "

value1=5;value2=7
subtraction=$( echo $value1-$value2 |bc )
echo "$value1 - $value2 = $subtraction "
subtraction=$( echo $value2-$value1 |bc )
echo "$value2 - $value1 = $subtraction "

value1=5;value2=7
division=$( echo $value1/$value2 |bc )
echo "$value1 / $value2 = $division "
division=$( echo $value2/$value1 |bc )
echo "$value2 / $value1 = $division "

value1=5;value2=7
multiplication=$( echo $value1*$value2 |bc )
echo "$value1 * $value2 = $multiplication "

value1=5;value2=7
modulus=$( echo $value1%$value2 |bc )
echo "$value1 % $value2 = $modulus "
modulus=$( echo $value2%$value1 |bc )

echo "$value2 % $value1 = $modulus "

References :- http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml


No comments:

Post a Comment