Wednesday, November 19, 2014

Shell Script To Simulate a Simple Calculator

  1. #!/bin/bash
  2. # Shell Program to simulate a simple calculator
  3. # --------------------------------------------------------------------
  4. # This is a free shell script under GNU GPL version 2.0 or above
  5. # Copyright (C) 2005 nixCraft project.
  6. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11.  
  12. a=$1
  13. op="$2"
  14. b=$3
  15.  
  16. if [ $# -lt 3 ]
  17. then
  18. echo "$0 num1 opr num2"
  19. echo "opr can be +, -, / , x"
  20. exit 1
  21. fi
  22.  
  23. case "$op" in
  24. +) echo $(( $a + $b ));;
  25. -) echo $(( $a - $b ));;
  26. /) echo $(( $a / $b ));;
  27. x) echo $(( $a * $b ));;
  28. *) echo "Error ";;
  29. esac

0 comments: