Input two numbers and add them when “a” is typed, subtract when “s” is typed Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Input two numbers and add them when “a” is typed, subtract when “s” is typed without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So I’m having a little trouble with this code. When i try to execute I get the message line 12: 0: command not found


#!/bin/bash

let results=0;

echo "First number please"

read num1

echo "Second mumber"

read num2

echo "Enter operation a=add, s=subtract, m=multiply, and d=divide"

read Op

if [ "$Op" = "a" ]

then

results=$((num1+num2))

elif [ "$Op" = "s" ]

then

results=$((num1-num2))

elif [ "$Op" = "d" ]

then

results=$((num1/num2))

elif [ "$Op" = "m" ]

then

results=$((num1*num2))

fi

Answer

I changed your shell script to the following code and it works, anyway division by zero bug does still exists:

#!/bin/bash

let results=0;
echo "First number please"
read num1
echo "Second number"
read num2
echo "Enter operation a=add, s=subtract, m=multiply, and d=divide"
read Op
if [ "$Op" = "a" ] ; then
    results=`echo "$num1+$num2" |bc `
elif [ "$Op" = "s" ]; then 
    results=`echo "$num1-$num2" |bc `
elif [ "$Op" = "d" ]; then 
    results=`"$num1/$num2" |echo bc`
elif [ "$Op" = "m" ] ; then 
    results=`echo "$num1*$num2"|bc`
else 
    echo "Enter operation a=add, s=subtract, m=multiply, and d=divide"
    read Op
fi;
echo $results
We are here to answer your question about Input two numbers and add them when “a” is typed, subtract when “s” is typed - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji