Linux Bash Scripting Tutorial
Arithmetic expressions
---------------------------------
Arithmetic expansion provides powerful tool for performing(integer) arithmetic operations in scripts. Bash has built-in support for Integer Arithmetic Operations.
Using external commands floating point arithmetic operations can be done.
translating string into numerical exansion is relatively straightforward using
backquotes, double parenthesis or using let command.
Bash Shell provides 4 different ways to do Integer Arithmetic operations
Bash Basic arithmetic Operations
Basic arithmetic operations can be performed on typed integer data type,without using let,expr commands
$ ~> declare -i i=11 $ ~> i=i+1; echo $i $ ~> i=i-1;echo $i $ ~> i=i*2;echo $i $ ~> i=i/2;echo $i $ ~> i=i**2;echo $i
let command evaluates the arithmetic operation.
$ ~> let i=0 $ ~> let "i=i+1" $ ~> echo $i #outputs 1 #in above case variable i intialized to 0 $ ~> let "i=i+1" $ ~> echo $i #outputs 1 #In this case also output will be 1, because unitialized variables by default set to "null", #Spaces can be included within let command expression $ ~> let "i = i + 1" # spaces included before and after = sign and with in the expression, note arithmetic expression should in quotation $ ~> echo $i #outputs 1
Operator | Arithmetic operation | Variable Substitution |
+ | $(( 2+2)) 4 | $v1=2;v2=2; $echo $(($v1+$v2)) 4 |
- | $(( 2-2)) 0 | $v1=2;v2=2; $echo $(($v1-$v2)) 0 |
* | $(( 2*2)) 4 | $v1=2;v2=2; $echo $(($v1*$v2)) 4 |
/ | $(( 2/2)) 1 | $v1=2;v2=2; $echo $(($v1/$v2)) 1 |
%(Modulus) | $(( 2%2)) 0 | $v1=2;v2=2; $echo $(($v1%$v2)) 0 |
** (Exponent) | $(( 2 ** 2)) 4 | $v1=2;v2=2; $echo $(($v1**$v2)) 1 |
Arithmetic expansion allows the evaluation of an arithmetic expression and substitues the result.
expr command evaluates arithmetic operations and does variable substritution. it's old style of doing arithmetic operations in bash.
Not all operators are suported by expr command.
Operator | Arithmetic operation | Variable Substitution |
+ | $expr 10 + 20 30 | $v=10 $v=`expr $v + 20` 20 |
- | $expr 10 - 20 -10 | $v=10 $v=`expr $v - 20` -10 |
* | $expr 10 \* 20 200 | $v=10 $v=`expr $v \* 20` 200 |
/ | $expr 10 / 20 0 | $v=10 $v=`expr $v / 20` 0 |
% | $expr 10 % 20 10 | $v=10 $v=`expr $v % 20` 10 |
** | $expr 10 ** 2 expr: syntax error: unexpected argument ‘!’ |
bc is precision based arithmetic calculator. It has its own programming constructs and math library.
Operator | Arithmetic operation | Variable Substitution |
+ | $bc<<< "10.5 + 20" 30.5 | $v1=99.99;v2=100.99 $bc <<< "$v1+$v2" 200.98 |
- | $bc<<< "10.5 - 20" -9.5 | $v1=99.99;v2=100.99 $bc <<< "$v1-$v2" -1.00 |
* | $bc<<< "10.5 * 20" 210.5 | $v1=99.99;v2=100.99 $bc <<< "$v1*$v2" 10097.99 |
/ | $bc<<< "10.5 / 20" 0 | $v1=99.99;v2=3 $bc <<< "scale=3;$v1/$v2" 3.333 |
%(Modulus) | $bc<<< "10.5 % 20" 10.5 | $v1=99.99;v2=100.99 $bc <<< "scale=3;$v1%$v2" .00990 |
^ (Exponent) | $bc<<< "10.5 ^ 2" 10.5 | v1=99.99;v2=2 $bc <<< "$v1^$v2" 9998.00 |
default all numbers are in Decimal. Using bc calculator programmer can convert numbers to Other base Numbers.
converting decimal number to Binary
echo "obase=2; 10" | bc
1010
echo "obase=2; 255" | bc
11111111
converting decimal number to HexaDecimal
echo "obase=16; 14" | bc
E
echo "obase=16; 255" | bc
FF
converting decimal number to Octal
echo "obase=8; 9" | bc
11
echo "obase=8; 255" | bc
377
Desktop calculator dc is a precision based calculator, uses post-fix notation for calculations. dc reads commands from the standard input, dc can also read from file and executes them. All output will be on standard ouput. Post fix notaions operators follow operands. a+b written as ab+ , previous notation is called as infix notaion, later is called as post-fix noation. Post-fix notaions perform better compared to infix notation.
for ex: infix notation a-b*c, can be written in post-fix as bc*a-
dc notation uses character "p" for printing the result and "q" for quitting the dc program
Operators follow operands. here enter 2 operands seperated by space or new line 10.5 1 then enter operator + finally for printing the result enter p, press enter , simlilary for other calculations
$ ~> dc 10.5 1 + p 11.5 10.5 2 - p 8.5 10.5 2 * p 21.0 10.5 2 / p 5 10.5 2 k 3 / p 3.50 q
Arthimetic comparisions has its own set of operators for comparisions. it differs from String and file comparisions.
These are called as relational operators for comparing numeric data. Every test condition returns True or False.
Following examples use && , || relational operators.
&& operator usage [ test condition ] && action; if test condition is true,then Action will be executed.
|| Operator usage: [ test condition ] || action
if test condition is false then action gets executed.
These are the special syntax in bash , test and execute commands. In either case action part is executed then exit status($?) is 0 otherwise 1.
Operator | Description |
-eq | For Equality testing for Ex: $~> [ 10 -eq 10 ] && echo "both are equal" $~> num=10; $~> [ 10 -eq $num ] && echo "both are equal" |
-ne | For Not Equality testing for ex: num=11 [ 10 -nq $num ] && echo "both are not equal" |
-lt | For less than testing |
-le | For less than or equal to testing |
-gt | For greater than testing for Ex: $~> [ 10 -gt 0 ] && echo "10 is greater" $~> [ -10 -gt 0 ] || echo "0 is greater than -10" |
-ge | For greater than or equal to testing |
This example converts Farenheit to Celcius and Celcius to Farenheit .It uses bc calculator for floating point Arithmetic operations.
#!/usr/bin/bash # Temperature Convertor from Fahrenheit to Celcius or Vice versa # # Formaula: c/5 = (f-32)/9 # # Using bc calculator # read -p "please enter Fahreinheit: " f c=`echo "scale=5;((($f-32)/9)*5)" | bc` echo echo -e "Fahreinheit $f\u2218 represented in Celcius: $c\u2103C" echo echo '*******************************************' read -p "please enter Celcius: " c f=`echo "scale=5; (($c/5)*9)+32" | bc ` echo -e "Celcius $c\u2218 represented in Fahreinheit : $f\u2109F " echo '*******************************************'
ADS