Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to create a calculator:

echo "What is your number?"
read n1 

echo "what is your second number?"
read n2

echo "what do you want to do?"
echo "1. add"
echo "2. subtract"
echo "3. divide"
echo "4. multiply"
read ans


if 
ans=$(( $n1+$n2 )); then
echo $ans

elif
ans=$(( $n1-$n2 )); then
echo $ans

elif
ans=$(( $n1/$n2 )); then
echo $ans

elif
ans=$(( $n1*$n2 )); then
echo $ans

else

But when I insert letters it shows me 0. How can I improve it? Also it gives me sometimes wrong answers.

share|improve this question
    
You don't employ the $sign variable anywhere... And you would be better off using a case statement than all that if..elif... logic. –  jasonwryan Jun 10 at 6:37
    
ok I changed it ans –  Invoker Jun 10 at 6:38
    
You certainly can do this. However, you'd be much better off using a high-level scripting language like Python for this sort of thing. –  Qudit Jun 10 at 7:07

1 Answer 1

up vote 2 down vote accepted

Try to use while loop :

input="yes"
while [[ $input = "yes" ]]
do

    PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for division: "
    select math in Addition Subtraction Multiplication Division
    do
        case "$math" in
        Addition)
            echo "Enter first no:"
            read num1
            echo "Enter second no:"
            read num2
            result=`expr $num1 + $num2`
            echo Answer: $result
            break
        ;;
        Subtraction)
            echo "Enter first no:"
            read num1
            echo "Enter second no:"
            read num2
            result=`expr $num1 - $num2`
            echo Answer: $result
            break
        ;;
        Multiplication)
            echo "Enter first no:"
            read num1
            echo "Enter second no:"
            read num2
            result=`expr $num1 * $num2`
            echo Answer: $result
            break
        ;;
        Division)
            echo "Enter first no:"
            read num1
            echo "Enter second no:"
            read num2
            result=$(expr "scale=2; $num1/$num2" | bc)
            echo Answer = $result
            break
        ;;
        *)
            echo Choose 1 to 4 only!!!!
            break
        ;;
    esac
    done

done
share|improve this answer
2  
Some questions about this one: 1) Why the input=yes; while [[ $input = "yes" ]] instead of while true? 2) Why the while... with all the breaks. When all the breaks are removed you can get rid of the whole while... routine. And, BTW, Multiplication should be expr $num1 \* $num2 –  Lambert Jun 10 at 8:36
    
And in addition to the answer, the OP asks also about entering non-numeric characters. See stackoverflow.com/questions/806906/… to deal with that. –  Lambert Jun 10 at 9:01
    
@terdon, I understand the script and therefore the underlying question why to use break and circumvent that with the while loop. The manual already mentions that 'The list is executed after each selection until a break command is executed'. To clean up the code you could get rid of the breaks and the enclosed while loop. –  Lambert Jun 10 at 10:31
    
@Lambert oh! You're quite right, my mistake. I am not very familiar with select. –  terdon Jun 10 at 10:34
    
If you think of further improving it just check out zenity. So you can make GUI for it out of shell script itself. –  user149704 Jun 12 at 2:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.