Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- numOfArgs=$#
- arg1=$1
- arg2=$2
- exitScript () {
- echo "End of the program"
- exit 0
- }
- runScript () {
- # echo "The decimal number 45 is transferred into the binary representation 101101"
- number=$arg2
- case $arg1 in
- "") showPromptError ;;
- "D") convertFromDecimal ;;
- "O") convertFromOctal ;;
- "H") convertFromHexa ;;
- *) showPromptError
- esac
- }
- validateArguments () {
- if [[ ("$numOfArgs" = 0) ]]; then
- echo 'no arguments passed, switch to user mode'
- return 1
- else
- isValid $arg1 ^[A-Za-z]+$
- isArg1Valid=$?
- isValid $arg2 ^-?[A-Za-z0-9]+$
- isArg2Valid=$?
- if [[ ! ("$numOfArgs" = 2 && $isArg1Valid = 1 && $isArg2Valid = 1 ) ]]; then
- echo 'Please pass two valid arguments'
- exitScript
- fi
- echo 'arguments passed, switching to script mode'
- return 0
- fi
- }
- isValid () {
- if [[ $1 =~ $2 ]]; then
- return 1
- else
- return 0
- fi
- }
- convertFromDecimal () {
- isValid $number ^-?[0-9]+$
- result=$?
- if [ $result = 1 ]; then
- task="decimal"
- convertToBinary $number
- else
- echo "Wrong decimal input"
- fi
- }
- convertFromOctal () {
- isValid $number ^[1-7][0-7]*$
- result=$?
- if [ $result = 1 ]; then
- task="octal"
- number=$(printf "%d\n" $number)
- convertOctalToDezimal $number
- else
- echo "Wrong octal input"
- fi
- }
- convertFromHexa () {
- isValid $number ^[0-9A-Fa-f]+$
- result=$?
- if [ $result = 1 ]; then
- task="hexal"
- number=$(printf "%d\n" 0x$number)
- convertToBinary $number
- else
- echo "Wrong hexal input"
- fi
- }
- convertToBinary () {
- local binary
- local num
- for((num=$number;num;num/=2))
- do
- binary=$((num%2))$binary
- done
- if [ $mode = 1 ]; then
- printf "The binary representation is %07d\n" $binary
- else
- printf "The %s number %s is transferred into the binary representation %07d\n" $task $arg2 $binary
- fi
- }
- convertOctalToDezimal () {
- local dez
- local num
- for((num=$number;num;num/=10))
- do
- dez=$((num%8))$dez
- done
- if [ $mode = 1 ]; then
- printf "The binary representation is \n" $dez
- else
- printf "The %s number %s is transferred into the binary representation \n" $dez
- fi
- convertToBinary $dez
- }
- showPromptError () {
- echo "Wrong input"
- }
- showPrompt () {
- tasks="Decimal Octal Hexadecimal Exit";
- PS3="Select one option:"
- select task in $tasks
- do
- case $task in
- "") showPromptError ;;
- "Decimal")
- echo "Enter the DECIMAL number you would like to transfer?"
- read number
- convertFromDecimal ;;
- "Octal")
- echo "Enter the OCTAL number you would like to transfer?"
- read number
- convertFromOctal ;;
- "Hexadecimal")
- echo "Enter the HEXADECIMAL number you would like to transfer?"
- read number
- convertFromHexa ;;
- "Exit") break ;;
- *) break
- esac
- done
- }
- #
- # proc.sh
- # Hier ist der aktuelle Einstiegspunkt des Skripts.
- #
- #clear
- echo "Programm started"
- validateArguments
- mode=$?
- if [ $mode = 0 ]; then
- runScript
- elif [ $mode = 1 ]; then
- showPrompt
- else
- exitScript
- fi
- exitScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement