Script to display sum of two number and to do calculations such as +, -, / etc

by on April 4, 2008 · 5 comments

  1. #!/bin/bash
  2. # Script to display sum of two number and to do calculations such as +, -, / etc
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # ----------------------------------------------------------------------
  10. echo "**** My calculator ****"
  11. echo "M A I N - M E N U"
  12. echo "1. Multiplication"
  13. echo "2. Subtraction"
  14. echo "3. Remainder"
  15. echo "4. Divide"
  16. echo -n "Please select your choice (1-4) : "
  17. read choice
  18.  
  19. echo -n "Enter your first number : "
  20. read n1
  21. echo -n "Enter your second number : "
  22. read n2
  23.  
  24. if [ $choice -eq 1 ]
  25. then
  26. answer="$n1 x $n2 = $(( $n1 * $n2 ))"
  27. elif [ $choice -eq 2 ]
  28. then
  29. answer="$n1 - $n2 = $(( $n1 - $n2 ))"
  30. elif [ $choice -eq 3 ]
  31. then
  32. answer="$n1 % $n2 = $(( $n1 % $n2 ))"
  33. elif [ $choice -eq 4 ]
  34. then
  35. answer="$n1 / $n2 = $(( $n1 / $n2 ))"
  36. else
  37. echo "Sorry please select number between 1-4 only"
  38. exit 1
  39. fi
  40. echo $answer


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • OPEOLUWA

    THIS REALY IS OF HELP TO ME
    THANKS GUY

  • Mohamed

    Hi ! everything is working fine. Thanks
    but when i try to print * . it prints all the files in the current directory. I am new to Unix so please help me out.
    Thanks :)

  • dhruv

    hi this is good but i am getting only 0 in division ….

  • bikash

    this is modular division not division

  • Rendy

    why ,if I input -1 + 0.009 = -,991 . Where is these zero??

Previous Script:

Next Script: