-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_calculator.sh
More file actions
36 lines (34 loc) · 854 Bytes
/
04_calculator.sh
File metadata and controls
36 lines (34 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<<Documentation
NAME : V. Karthikeyan
DATE : 09.05.2021
DESCRIPTION : Write a script for arithmetic calculator using command line arguments
INPUT : ./04_calculator.sh 54 / 69
OUTPUT : 54 / 69 = .78
Documentation
#!/bin/bash
if [ $# -eq 0 ] #if no arguments passed -->exit
then
echo "Please pass the arguments through command line"
exit
fi
if [ $# -lt 3 ] #if less then three arguments passed -->exit
then
echo "Please pass 3 arguments"
exit
fi
case $2 in #check the operator in $2
+)ans=`expr $1 + $3 #addition
echo "$@ = $ans"
;;
-)ans=`expr $1 - $3` #subtraction
echo "$@ = $ans"
;;
x)ans=$(($1*$3)) #multiplication
echo "$@ = $ans"
;;
/)ans=`echo "scale=2 ; (($1/$3))" | bc` #division
echo "$@ = $ans"
;;
*)echo "Please enter a valid operator" #aside from four operator -->error message
;;
esac