-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_to_dec.sh
executable file
·71 lines (63 loc) · 1.85 KB
/
bin_to_dec.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
######################################################################
# Convert binary number to decimal
######################################################################
set -o nounset
set -o errexit
######################################################################
# Validate script argument. It's valid if it's a interger decimal
# number or -h or --help string.
# Globals:
# none
# Arguments:
# $@ as all arguments passed to script
# Outputs:
# return 0 when argument is valid otherwise 1
######################################################################
input_check() {
if [[ ${#@} -eq 1 ]] && [[ $1 =~ (^-h$)|(^--help$)|(^1{1}[0-1]{0,19}$) ]]; then
return 0
else
return 1
fi
}
print_error() {
printf "Valid input is one positive binary number or -h or --help for help\n"
}
######################################################################
# Print help if script run with -h or --help argument
# Globals:
# none
# Arguments:
# none
# Outputs:
# Print to stdout help message and exit.
######################################################################
print_help() {
printf "%s\n%s\n%s\n",\
"Convert entered number from binary to decimal."\
"Use $./dec_to_bin.sh 111101010101 for normal use or enter arguments"\
"-h, --help to dispaly this help and exit."
}
######################################################################
# Get decimal number from converted binary.
# Globals:
# None
# Arguments:
# $1 as binary number
# Outputs:
# Print decimal number to stdout
######################################################################
get_decimal() {
len=${#1}
out=0
for ((i = 0; i < $len; i++)); do
out=$((out + $((${1:$((len-len+i)):1} * 2 ** $((len-i-1))))))
done
echo "$out"
}
main() {
input_check "$@" || { print_error; exit 1; }
get_decimal "$@"
}
main "$@"