Advertisement
Guest User

vitobash

a guest
Nov 28th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. numOfArgs=$#
  4. arg1=$1
  5. arg2=$2
  6.  
  7. exitScript () {
  8. echo "End of the program"
  9. exit 0
  10. }
  11.  
  12. runScript () {
  13. #   echo "The decimal number 45 is transferred into the binary representation 101101"
  14.     number=$arg2
  15.  
  16.     case $arg1 in
  17.         "") showPromptError ;;
  18.         "D") convertFromDecimal ;;
  19.         "O") convertFromOctal ;;
  20.         "H") convertFromHexa ;;
  21.          *)  showPromptError
  22.     esac
  23. }
  24.  
  25. validateArguments () {
  26. if [[ ("$numOfArgs" = 0) ]]; then
  27.     echo 'no arguments passed, switch to user mode'
  28.     return 1
  29. else
  30.     isValid $arg1 ^[A-Za-z]+$
  31.     isArg1Valid=$?
  32.     isValid $arg2 ^-?[A-Za-z0-9]+$
  33.     isArg2Valid=$?
  34.     if [[ ! ("$numOfArgs" = 2 && $isArg1Valid = 1 && $isArg2Valid = 1 ) ]]; then
  35.         echo 'Please pass two valid arguments'
  36.             exitScript
  37.     fi
  38.  
  39.     echo 'arguments passed, switching to script mode'
  40.     return 0
  41. fi
  42. }
  43.  
  44. isValid () {
  45. if [[ $1 =~ $2 ]]; then
  46. return 1
  47. else
  48. return 0
  49. fi
  50. }
  51.  
  52. convertFromDecimal () {
  53. isValid $number ^-?[0-9]+$
  54. result=$?
  55. if [ $result = 1 ]; then
  56.     task="decimal"
  57.     convertToBinary $number
  58. else
  59.     echo "Wrong decimal input"
  60. fi
  61. }
  62. convertFromOctal () {
  63. isValid $number ^[1-7][0-7]*$
  64. result=$?
  65. if [ $result = 1 ]; then
  66.     task="octal"
  67.     number=$(printf "%d\n" $number)
  68.     convertOctalToDezimal $number
  69. else
  70.     echo "Wrong octal input"
  71. fi
  72. }
  73. convertFromHexa () {
  74. isValid $number ^[0-9A-Fa-f]+$
  75. result=$?
  76. if [ $result = 1 ]; then
  77.     task="hexal"
  78.     number=$(printf "%d\n" 0x$number)
  79.     convertToBinary $number
  80. else
  81.     echo "Wrong hexal input"
  82. fi
  83. }
  84.  
  85. convertToBinary () {
  86. local binary
  87. local num
  88. for((num=$number;num;num/=2))
  89.     do
  90.         binary=$((num%2))$binary
  91. done
  92.  
  93. if [ $mode = 1 ]; then
  94. printf "The binary representation is %07d\n" $binary
  95. else
  96. printf "The %s number %s is transferred into the binary representation %07d\n" $task $arg2 $binary
  97. fi
  98. }
  99.  
  100.  
  101. convertOctalToDezimal () {
  102. local dez
  103. local num
  104. for((num=$number;num;num/=10))
  105.     do
  106.         dez=$((num%8))$dez
  107. done
  108.  
  109. if [ $mode = 1 ]; then
  110. printf "The binary representation is \n" $dez
  111. else
  112. printf "The %s number %s is transferred into the binary representation \n"  $dez
  113. fi
  114.  
  115. convertToBinary $dez
  116.  
  117. }
  118.  
  119. showPromptError () {
  120. echo "Wrong input"
  121. }
  122. showPrompt () {
  123. tasks="Decimal Octal Hexadecimal Exit";
  124.  
  125. PS3="Select one option:"
  126. select task in $tasks
  127. do
  128.    case $task in
  129.     "") showPromptError ;;
  130.     "Decimal")
  131.         echo "Enter the DECIMAL number you would like to transfer?"
  132.         read number
  133.         convertFromDecimal ;;
  134.  
  135.     "Octal")
  136.         echo "Enter the OCTAL number you would like to transfer?"
  137.         read number
  138.         convertFromOctal ;;
  139.  
  140.     "Hexadecimal")
  141.         echo "Enter the HEXADECIMAL number you would like to transfer?"
  142.         read number
  143.         convertFromHexa ;;
  144.  
  145.     "Exit") break ;;
  146.      *)  break
  147.    esac
  148. done
  149. }
  150.  
  151. #
  152. # proc.sh
  153. # Hier ist der aktuelle Einstiegspunkt des Skripts.
  154. #
  155.  
  156. #clear
  157. echo "Programm started"
  158.  
  159. validateArguments
  160. mode=$?
  161.  
  162. if [ $mode = 0 ]; then
  163. runScript
  164. elif [ $mode = 1 ]; then
  165. showPrompt
  166. else
  167. exitScript
  168. fi
  169.  
  170. exitScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement