Sem 3‎ > ‎OS (Unix) LAB‎ > ‎

D5: expr

posted Oct 9, 2011, 2:36 AM by Neil Mathew   [ updated Oct 9, 2011, 5:47 AM ]
It is used to do math in the shell.

Syntax:

<variable_name>=`expr <operand> <operator> <operand> `


Example: 

sum=`expr $a + $b + $c + $d + $e`

sum=`expr 2 + 4`

My Mistakes


1. No Space should be there between <Variable> = `

sum= `expr $a + $b + $c + $d + $e` X ERROR


sum =`expr $a + $b + $c + $d + $e` X ERROR


sum=`expr $a + $b + $c + $d + $e` RIGHT


But, this is fine:

sum=`     expr $a + $b + $c + $d + $e` RIGHT



2. Space must be there between the operator and operand. It is taken as string otherwise.


sum=`echo "Enter 5 numbers: "
read a
read b
read c
read d
read e

sum=`expr $a+$b+$c+$d+$e`

echo "The Sum is: $sum "


OUTPUT:

Enter 5 numbers:
6
7
5
4
3
The Sum is: 6+7+5+4+3




3. Expr must contain Integer values if written without extra commands.


(Non-integer)

area=` expr 22.0 / 7.0 \* $r \* $r `

expr: non-integer argument

(Corrected)

area=` expr 22 / 7 \* $r \* $r `

Enter the radius of the Circle:
1
Ans is 3






4. the ` symbol before and after the expr line IS NOT apostrophe.




sum=`expr $a + $b + $c + $d + $e` RIGHT



Comments