Advertisement
solidsnake

OS Converter Exercise (With program parameters)

Feb 10th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.40 KB | None | 0 0
  1. #Author -- MR
  2. #Date modified -- 2/11/14
  3.  
  4. fah_to_cel()
  5. {
  6.     echo
  7.     echo -n "Enter fahrenheit: "
  8.     read fah
  9.     echo
  10.     a=32
  11.     b=0.56
  12.     echo -n "$fah FAHRENHEIT in CELSIUS is: "
  13.     echo -n "$fah $a $b" | awk '{ print ($1-$2) * $3 }'
  14.     echo
  15. }
  16.  
  17. cel_to_fah()
  18. {
  19.     echo
  20.     echo -n "Enter celsius: "
  21.     read cel
  22.     echo
  23.     c=1.8
  24.     d=32
  25.     echo -n "$cel CELSIUS in FAHRENHEIT is: "
  26.     echo -n "$cel $c $d" | awk '{ print ($1*$2) + $3 }'
  27.     echo
  28. }
  29.        
  30. help()
  31. {
  32.     echo
  33.     echo "PROGRAM DESCRIPTION"
  34.     echo
  35.     echo "This program converts temperature from C to F, vice versa"
  36.     echo
  37. }
  38.  
  39. menu()
  40. {
  41. while [ "$choice" != "3" ]
  42. do
  43.     echo
  44.     echo "~~~~What do you want to do?~~~~"
  45.     echo "================================"
  46.     echo "1. Convert Fahrenheit to Celsius"
  47.     echo "2. Convert Celsius to Fahrenheit"
  48.     echo "3. Exit"
  49.     echo "================================"
  50.     echo
  51.     echo -n "Enter choice: "
  52.     read choice
  53.     echo
  54.        
  55.     if [ "$choice" == "1" ]
  56.     then
  57.         fah_to_cel
  58.     elif [ "$choice" == "2" ]
  59.     then
  60.         cel_to_fah
  61.     elif [ "$choice" == "3" ]
  62.     then
  63.         echo "Goodbye"
  64.         echo
  65.     else
  66.         echo "Invalid choice"
  67.     fi
  68. done
  69. }
  70.  
  71.  
  72. if [ "$#" -eq 0 ]    
  73. then
  74.     menu
  75. else
  76.     while getopts hcfi j
  77.         do
  78.             case $j in
  79.                 h) help;;
  80.                 c) cel_to_fah;;
  81.                 f) fah_to_cel;;
  82.                 i) menu;;
  83.             esac
  84.         done
  85. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement