ARITHMETIC OPERATIONS USING SHELL PROGRAMMING
(Using SWITCH - CASE)
To
write a Shell Program to perform Arithmetic Operations using Switch – Case
ALGORITHM:
Step 1: Start
Step 2: Read the two Numbers.
Step 3: Get the operation choice from the User
Step 4: Give the expressions for each case and solve them.
Step 5: Print the Result
Step 6: Stop
SHELL
PROGRAM:
echo "Enter Two Numbers"
read a b
echo "What do you want to do? (1 to 5)"
echo "1) Sum"
echo "2) Difference"
echo "3) Product"
echo "4) Quotient"
echo "5) Remainder"
echo "Enter your Choice"
read n
case "$n" in
1) echo "The Sum of $a and $b is `expr $a +
$b`";;
2) echo "The Difference between $a and $b is
`expr $a - $b`";;
3) echo "The Product of the $a and $b is `expr
$a \* $b`";;
4) echo "The Quotient of $a by $b is `expr $a
/ $b`";;
5) echo "The Remainder of $a by $b is `expr $a
% $b`";;
esac
SAMPLE
OUTPUT:
[Cseb19@sathish-desktop]# sh
arith_switch.sh
Enter Two Numbers
12 10
What do you want to do? (1 to 5)
1) Sum
2) Difference
3) Product
4) Quotient
5) Remainder
Enter your Choice
4
The Quotient of 12 by 10 is 1
RESULT:
Thus
the Shell program to perform arithmetic operations using Switch Case was
executed and the output was verified.
#!/bin/bash
ReplyDeleteclear
n=0
echo "enter a:"
read a
echo "enter b:"
read b
echo "enter the option"
echo "Add"
echo "sub"
echo "divide"
echo "multiply"
read n
case "$n" in
"1") echo "`expr $a + $b`";;
"2") echo "`expr $a - $b`";;
"3") echo "`expr $a / $b`";;
"4") echo "`expr $a \* $b`";;
esac
output :
enter a:
2
enter b:
2
enter the option
Add
sub
divide
multiply
2
0
rashmi@rashmi-Inspiron-3558:~$ nano case.sh
Use "fg" to return to nano.
[1]+ Stopped nano case.sh
rashmi@rashmi-Inspiron-3558:~$ nano case.sh
rashmi@rashmi-Inspiron-3558:~$
rashmi@rashmi-Inspiron-3558:~$ nano case.sh
rashmi@rashmi-Inspiron-3558:~$ ./case.sh
enter a:
5
enter b:
5
enter the option
Add
sub
divide
multiply
4
25