Connecting Tech Pros Worldwide Forums | Help | Site Map

bash script of a simple calculator

lifeisgreat20009's Avatar
Member
 
Join Date: Oct 2007
Posts: 70
#1: Jan 4 '09
Expand|Select|Wrap|Line Numbers
  1. Echo “Enter first number:”
  2. Read n1
  3. Echo “Enter Second number:”
  4. Read n2
  5. Echo “Enter operation to be carried out that is +, - , / or X”
  6. Read opr
  7. If [ opr==”+” ]
  8. Then
  9. echo $(( $a + $b ))
  10. elif [ opr==”-“]
  11. then
  12. echo $(( $a - $b ))
  13. elif [ opr==”/” ]
  14. then
  15. echo $(( $a / $b )) 
  16. elif [ opr==”X” ]
  17. then
  18. echo $(( $a * $b ))
  19. fi
  20.  
Is the script above correct syntactically ?? its not working fine. I searched net for syntax errors but all seems to be correct. Plz tell me where modifications are needed in the code ?
thnx and regards

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Jan 4 '09

re: bash script of a simple calculator


You're entering n1 and n2 and then operating on a and b
lifeisgreat20009's Avatar
Member
 
Join Date: Oct 2007
Posts: 70
#3: Jan 4 '09

re: bash script of a simple calculator


otherwise is it ok ??
I made changes here only by mistake..
Its a and b only in the original script
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#4: Jan 4 '09

re: bash script of a simple calculator


Sorry I'm not a bash expert it is just appeared to be an obvious mistake in any programming language.
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#5: Jan 5 '09

re: bash script of a simple calculator


Hi,
There are some couple of errors in the code .
check this
Expand|Select|Wrap|Line Numbers
  1. echo "Enter first number:" 
  2. read a 
  3. echo "Enter Second number:" 
  4. read b 
  5. echo "Enter operation to be carried out that is +, - , / or X" 
  6. read opr 
  7. #echo "opr =$opr"
  8. if [ $opr = "+" ] 
  9. then 
  10.     op=`expr $a + $b`
  11.     echo "$op"
  12. elif [ $opr = "-" ] 
  13. then 
  14.     op=`expr $a - $b`
  15.     echo "$op"
  16. elif [ $opr = "/" ] 
  17. then 
  18.     op=`expr $a / $b`
  19.     echo "$op"
  20. elif [ $opr = "X" ] 
  21. then 
  22.     op=`expr $a \* $b`
  23.     echo "$op"
  24. fi 
Raghu
lifeisgreat20009's Avatar
Member
 
Join Date: Oct 2007
Posts: 70
#6: Jan 5 '09

re: bash script of a simple calculator


thank you very much..its working now
Reply


Similar Unix / Linux / BSD bytes