Issue
#!/bin/bash
# your code goes here
echo "1.Addition 2.Subtraction 3.Multiplication 4.Division"
read n
echo "Enter the operends"
read a
read b
case $n in
"1") echo "$a+$b =`expr $a \ + $b`";;
"2") echo "$a-$b=`expr $a \ - $b`";;
"3") echo "$a*$b=`expr $a \ * $b`";;
"4") echo "$a/$b=`expr $a \ / $b`";;
esac
Ther errors are
expr: syntax error: unexpected argument ‘ +’
Whent the input is
1 1 1 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter the operends 1+1 = this is the output
Solution
The first problem you had were Windows line endings, dos2unix
fixed that.
The second problem is that you don't need to escape operators in expr
except for *
, it should be:
"1") echo "$a+$b =`expr $a + $b`";;
"2") echo "$a-$b=`expr $a - $b`";;
"3") echo "$a*$b=`expr $a \* $b`";;
"4") echo "$a/$b=`expr $a / $b`";;
Now your script will run ok:
$ ./main.sh
1.Addition 2.Subtraction 3.Multiplication 4.Division
1
Enter the operends
2
3
2+3 =5
$ ./main.sh
1.Addition 2.Subtraction 3.Multiplication 4.Division
2
Enter the operends
5
2
5-2=3
BTW, there are plenty of things wrong with the script, you should always check your scripts with shellcheck and fix all errors it reports.
Answered By - Arkadiusz Drabczyk Answer Checked By - Cary Denson (WPSolving Admin)