-
Notifications
You must be signed in to change notification settings - Fork 1
/
nespithefier.sh
executable file
·55 lines (52 loc) · 1.47 KB
/
nespithefier.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
#!/usr/bin/env bash
# Encode/decode any string Nespithe-way. Requires Bash shell (https://www.gnu.org/software/bash/)
#
# Examples:
# echo "no lyrics here" | nespithefier.sh
# nespithefier.sh file-to-be-mangled.txt
# echo "nespithe" | nespithefier.sh --reverse
#
# Updates can be found at https://github.com/AnttiBoman/demilich/blob/main/nespithefier.sh
#
# Licensed under the GNU General Public License v3.0 (https://github.com/AnttiBoman/demilich/blob/main/LICENSE)
# Argument handling stolen from https://medium.com/@Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
PARAMS=""
REVERSE=0
while (( "$#" )); do
case "$1" in
-r|--reverse)
REVERSE=1
shift
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
# Let's manglererify!!!
while IFS= read -r line ; do
line=$(tr -d '[:space:]' <<< $line)
final=""
while [ "$line" != "" ] ; do
if [ $REVERSE = 0 ] ; then
final=${line:0:3}"$final"
line=${line:3}
else
if (( ${#line} >= 3 )) ; then
final="$final"${line: -3}
line=${line: 0: $(( ${#line} - 3 )) }
else
final="$final"${line}
line=""
fi
fi
done
echo $final
done < "${1:-/dev/stdin}"