diff --git a/bin/NumberTea b/bin/NumberTea index 7b01d6a..c7cd9e3 100644 --- a/bin/NumberTea +++ b/bin/NumberTea @@ -1,7 +1,7 @@ #!/bin/bash # Number Translator -VERSION=02.23.24 +VERSION=03.05.24 # --- Functions --------------------------------------------------------------- @@ -63,10 +63,9 @@ toBin() { # arg1: a numeric value, arg2: value's numbering system. Echos the bin bin) echo $value ;; dec) - while [ $value -gt 0 ]; do - local remainder=$(( $value % 2 )) - local binary="$remainder$binary" - local value=$(( $value / 2 )) + while [[ $value -gt 0 ]]; do + binary=$(( value % 2 ))"$binary" + value=$(( value / 2 )) done echo $binary @@ -157,22 +156,18 @@ toHex() { # arg1: a numeric value, arg2: value's numbering system. Echos the hex ;; dec) - #local hexadecimal=$(toHex $(toBin $value "dec") "bin") while [[ $value -gt 0 ]]; do - local remainder=$((value % 16)) - if [[ $remainder -lt 10 ]]; then - hexadecimal="${remainder}${hexadecimal}" - else - case $remainder in - 10) hexadecimal="A${hexadecimal}" ;; - 11) hexadecimal="B${hexadecimal}" ;; - 12) hexadecimal="C${hexadecimal}" ;; - 13) hexadecimal="D${hexadecimal}" ;; - 14) hexadecimal="E${hexadecimal}" ;; - 15) hexadecimal="F${hexadecimal}" ;; - esac - fi - value=$((value / 16)) + local remainder=$((value % 16)) + case $remainder in + 10) hexadecimal="A${hexadecimal}" ;; + 11) hexadecimal="B${hexadecimal}" ;; + 12) hexadecimal="C${hexadecimal}" ;; + 13) hexadecimal="D${hexadecimal}" ;; + 14) hexadecimal="E${hexadecimal}" ;; + 15) hexadecimal="F${hexadecimal}" ;; + *) hexadecimal="$remainder${hexadecimal}" ;; + esac + value=$((value / 16)) done echo $hexadecimal @@ -268,57 +263,62 @@ runFile() { # arg1: path to a text file of BIN or TXT. Determins if file is stri # --- Arguments processing ---------------------------------------------------- if [ -z $1 ]; then - printf "ERROR: Not enough arguments, please provide a number type and a value to be translated.\n\n" - USAGE; exit 1 + USAGE; exit 1 fi # --- Options processing ------------------------------------------------------ while [[ "$1" =~ ^-.* ]]; do case $1 in - -b | --bin) shift - [[ ! $1 =~ ^[01]+$ ]] && printf "$1 is not binary.\n\n" && shift && continue + -b | --bin) + [[ ! $2 =~ ^[01]+$ ]] && printf "$2 is not binary.\n\n" && shift 2 && continue - bin=$1; dec=$(toDec $1 "bin"); hex=$(toHex $1 "bin") - printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" + bin=$2 + dec=$(toDec $2 "bin") + hex=$(toHex $2 "bin") + + printf "dec %d\nbin %s\nhex %s\n\n" "$dec" "$bin" "$hex" + shift 2 ;; - -d | --dec) shift - [[ ! $1 =~ ^[0-9]+$ ]] && printf "$1 is not decimal.\n\n" && shift && continue + -d | --dec) + [[ ! $2 =~ ^[0-9]+$ ]] && printf "$2 is not decimal.\n\n" && shift 2 && continue + + bin=$(toBin $2 "dec") + dec=$2 + hex=$(toHex $2 "dec") - bin=$(toBin $1 "dec"); dec=$1; hex=$(toHex $1 "dec") printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" + shift 2 ;; - -h | --hex) shift - [[ ! $1 =~ ^[0-9a-fA-F]+$ ]] && printf "$1 is not hexadecimal.\n\n" && shift && continue + -h | --hex) + [[ ! $2 =~ ^[0-9a-fA-F]+$ ]] && printf "$2 is not hexadecimal.\n\n" && shift 2 && continue + + bin=$(toBin $2 "hex") + dec=$(toDec $2 "hex") + hex=$(toHex $2 "hex") - bin=$(toBin $1 "hex"); dec=$(toDec $1 "hex"); hex=$(toHex $1 "hex") printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" + shift 2 ;; - -t | --text) shift - encoded="$1" - [[ $1 =~ ^[01\ ]+$ ]] && decoded=$(textTea "$1" "bin") || decoded=$(textTea "$1" "txt") + -t | --text) + encoded="$2" + [[ $2 =~ ^[01\ ]+$ ]] && decoded=$(textTea "$2" "bin") || decoded=$(textTea "$2" "txt") printf "encoded: ${encoded}\ndecoded: ${decoded}\n\n" + shift 2 ;; - -f | --file) shift - [[ ! -f $1 ]] && printf "Cannot read file $1.\n\n" && shift && continue - - runFile "$1" + -f | --file) + [[ ! -f $2 ]] && printf "Cannot read file $2.\n\n" && shift 2 && continue + runFile "$2" + shift 2 ;; - --help ) shift - USAGE - ;; - --version ) shift - printf "Script Version:\t$VERSION\n" - ;; - *) - printf "\n [!] Invalid option: $1\n\n" - USAGE; exit 1 - ;; - esac; shift + --help ) USAGE; exit 0 ;; + --version ) printf "Script Version:\t$VERSION\n"; exit 0 ;; + *) printf "\n [!] Invalid option: $1\n\n"; exit 1 ;; + esac; done