Advertisement
Guest User

iz2jffs calc

a guest
Nov 3rd, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.86 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #######################################################
  4. #     Bash CALCulator 2
  5. #######################################################
  6. #
  7. #
  8. #    FILE: bashcalc2.sh
  9. # VERSION: 1.1
  10. #    DATE: 06-19-2006
  11. #
  12. #  AUTHOR: Crouse - Please visit bashscripts.org and usalug.org
  13. #
  14. #  Z2 cosmetics: dronz#zipit
  15. ########################################################
  16.  
  17. header ()
  18. {
  19. clear
  20. echo "\033[1;33;42m          CALCULATOR          ";
  21. echo "      Help: H then Enter      ";
  22. echo "------------------------------\033[49m\033[0m";
  23. }
  24.  
  25. calc ()
  26. {
  27. header
  28. # Reset scale to 4 after Clear or Help.
  29. scale="scale=4"
  30. while true
  31. do read -p "" bashcalc;
  32.        case "$bashcalc" in
  33.        q|Q|1|quit|exit|$'\e') clear ; exit ;;
  34.        c|C|+) calc ;;
  35.        h|H|"'"|help|"?")
  36.           clear
  37.           echo ""
  38.           echo " [H] then Enter = this HELP"
  39.           echo " [C] then Enter = clear screen"
  40.           echo " [Q] then Enter = quit"
  41.           echo ""
  42.           echo " For simple calculations, "
  43.           echo " type your keyboard as usual."
  44.           echo " Allowed: 1234567890 . + - * /"
  45.           echo " SHIFT then , or . are  (  )"
  46.           echo " ALT then SHIFT then ; is  ^ "
  47.           echo ""
  48.           read -p  " Type Enter" temp;
  49.           clear
  50.           echo ""
  51.           echo " For heavy calculations,"
  52.           echo " toggle to the red chars:"
  53.           echo " SHIFT then SPACE = toggle ALT"
  54.           echo " SHIFT then ; = ^ = power"
  55.           echo " SHIFT then J or K = (  )"
  56.           echo " CTRL and . = dec. point"
  57.           echo ""
  58.           echo " Last calculation is stored in"
  59.           echo "         /tmp/calc "
  60.           echo ""
  61.           read -p  " Type Enter" temp;
  62.           calc
  63.           ;;
  64.  
  65.        # Change the decimal scale if you like...
  66.        scale*) scale="${bashcalc}" ;;
  67.  
  68.        # Reuse previous result if we start with an operator.
  69.        # To use a negative number start with a space first.
  70.        -*) bashcalc="${result}${bashcalc}" ;;
  71.        +*) bashcalc="${result}${bashcalc}" ;;
  72.        /*) bashcalc="${result}${bashcalc}" ;;
  73.        "*"*) bashcalc="${result}${bashcalc}" ;;
  74.        "%"*) bashcalc="${result}${bashcalc}" ;;
  75.        "^"*) bashcalc="${result}${bashcalc}" ;;
  76.  
  77.        # Just in case I cant remember the math fns...
  78.        # Reuse previous result if we start with a fn.
  79.        sqrt) bashcalc="sqrt(${result})" ;;
  80.        sin) bashcalc="s(${result})" ;;
  81.        atan) bashcalc="a(${result})" ;;
  82.        ln) bashcalc="l(${result})" ;;
  83.        exp) bashcalc="e(${result})" ;;
  84.        # ...
  85.        # We could do many calculator key fns like this.
  86.  
  87.        esac
  88.  
  89.   result=$(echo " ${scale}; ${bashcalc} " | bc -l )
  90.   echo ${result}
  91.   echo "------------------------------";
  92.   echo "${bashcalc} =" > /tmp/calc ;
  93.   echo "${result}" >> /tmp/calc ;
  94. done
  95. }
  96.  
  97. # Program run starts here
  98. calc
  99.  
  100. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement