1 Replies - 5116 Views - Last Post: 17 January 2013 - 12:34 PM Rate Topic: -----

#1 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Bash Scripting - making a calculator

Posted 17 January 2013 - 09:56 AM

am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed.

eg. 1 + 1 = or 1 + 1 + 2 + 1 =

Both of these should be possible.


read -p "what's the first number? " n1
PS3="what's the operation? "
select ans in add subtract multiply divide equals; do
case $ans in 
    add) op='+' ; break ;;
    subtract) op='-' ; break ;;
    multiply) op='*' ; break ;;
    divide) op='/' ; break ;;
    *) echo "invalid response" ;;
esac
done
read -p "what's the second number? " n2
ans=$(echo "$n1 $op $n2" | bc -l)
printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"

exit 0



This is what I have written so far, but i cannot work out how to make it possible to let the user choose 'equals' or to loop back round to enter another operation. Any ideas what I can do to my code here? I have been stuck on this all day.

I dont want the user to enter the equation themselves, i want them to be choosing from a list.

Is This A Good Question/Topic? 0
  • +

Replies To: Bash Scripting - making a calculator

#2 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Re: Bash Scripting - making a calculator

Posted 17 January 2013 - 12:34 PM

Basically I would like the script to
1 - Prompt the user for a number.
2 - Prompt the user for the operation they would like to perform, either add, subtract, multiply or divide.
3 - Prompt the user for a number.
4 - Prompt the user for the operation they would like to perform, either add, subtract, multiply, divide OR EQUALS.
5 - Display the result of the selected operation. The script should then exit or continue from step 3 if the user has not selected equals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1