Repository navigation
Expand file tree
/
Copy pathconvert.sh
More file actions
executable file
·105 lines (94 loc) · 2.69 KB
/
Copy pathconvert.sh
File metadata and controls
executable file
·105 lines (94 loc) · 2.69 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
if [ "$#" -lt 1 ]; then
echo "Usage: ./convert [mode] [operands...]
./convert help" >&2
exit 1
fi
help() {
mode=${1-}
case $mode in
bit)
echo "Usage: ./convert [xor|and] arg1 arg2
ARG FORMATS:
- If leading '0x', treated as hexadecimal : 0x01, 0x2, 0xff
- If leading zero, treated as binary : 01, 001, 0101
- If leading special or non-special character, treated as ASCII : aBc, BCa, z!&_
- If leading decimal except zero, treated as decimal : 123, 321, 990" >&2
exit 1
;;
*)
echo "Usage: ./convert help [MODE]
MODE:
- bit (bitwise operations: xor)
EXAMPLES:
./convert xor 0x123 ABCD
./convert and 01010111 776766" >&2
exit 1
;;
esac
}
arg_nbr_equals() {
# If total arg given != required nbr of args, print help
if ! [ $1 -eq $2 ]; then
help bit
fi
}
# Todo - Stronger checks if valid binary / valid hexa etc
arg_type() {
if [[ $1 =~ ^[0-9]+$ ]] ; then
echo 10
else
case $1 in
0x*)
echo 16
;;
0*)
echo 2
;;
[[:ascii:]]*)
echo "Error : ASCII need to be converted"
exit 1
;;
*)
echo "Error : could not identify argument type for $1"
exit 1
;;
esac
fi
}
xor() {
printf "%d\n" $(($(arg_type $1)#$1 ^ $(arg_type $2)#$2))
}
main() {
# Intuition :
#
# Case function 1 - - -v Case conversion 1 - -v
# ^ | ^ |
# | v | v
# Initial switchcase -> Sanitize args -> Store result(s) in array -> Handle output parameters -> Output
# & Convert args ^ | ^
# | | v |
# v | Case conversion 2 - -^
# Case function 2 - - -^
mode=${1-}
results=()
case $mode in
xor|and)
arg_nbr_equals $# 3 $mode
arg1=${2-}
arg2=${3-}
$mode $arg1 $arg2
;;
and)
;;
help)
mode=${2-}
help $mode
;;
*)
;;
esac
}
main $@