-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.sh
executable file
·92 lines (80 loc) · 2.04 KB
/
demo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/ksh
#
# Copyright (c) 2015 Tristan Le Guern <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
set -e
readonly PROGNAME="$(basename $0)"
readonly VERSION='v1.0'
usage() {
echo "usage: $PROGNAME [-e encoding] [-s string | file]"
}
file=""
eflag="base64"
sflag=""
while getopts ":e:s:" opt;do
case $opt in
e) eflag="$OPTARG";;
s) sflag="$OPTARG";;
:) echo "$PROGNAME: option requires an argument -- $OPTARG";
usage; exit 1;;
?) echo "$PROGNAME: unkown option -- $OPTARG";
usage; exit 1;;
*) usage; exit 1;;
esac
done
shift $(( $OPTIND - 1 ))
if [ -n "$1" ]; then
file="$1"
shift
fi
if [ $# -ge 1 ]; then
echo "$PROGNAME: invalid trailing chars -- $@"
usage
exit 1
fi
set -u
autoload base16_encode
autoload hex_encode
autoload base32_encode
autoload base32hex_encode
autoload base64_encode
autoload base64url_encode
case "$eflag" in
hex|base16|base32|base32hex|base64|base64url) :;;
*) echo "$PROGNAME: invalid encoding algorithm -- $eflag";;
esac
if [ -n "$sflag" ] && [ -n "$file" ]; then
usage
exit 1
fi
if [ -z "$sflag" ] && [ -z "$file" ]; then
usage
exit 1
fi
if [ -n "$file" ]; then
if [ -f "$file" ]; then
#
# Converts \n to \f to prevent the shell from
# stripping them
#
OLDIFS="$IFS"
IFS=''
sflag="$(cat $file | tr '\n' '\f')"
IFS="$OLDIFS"
else
echo "$PROGNAME: $file: No such file"
fi
fi
${eflag}_encode "$sflag"