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

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

Leave a Reply

Your email address will not be published. Required fields are marked *