summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--caesar.sh25
1 files changed, 25 insertions, 0 deletions
diff --git a/caesar.sh b/caesar.sh
new file mode 100644
index 0000000..186891d
--- /dev/null
+++ b/caesar.sh
@@ -0,0 +1,25 @@
+number=13
+
+while getopts n: opt; do
+ case $opt in
+ n) number=${OPTARG};;
+ ?) exit 1;;
+ esac
+done
+
+shift $((OPTIND - 1))
+
+if [ $# -ne 0 ]; then
+ printf 'usage: %s [-n number]\n' "$0"
+ printf '\t-n the number of characters to rotate (default 13)\n'
+ exit 1
+fi
+
+number=$((number % 26))
+A=$(printf '%d' 0$(printf '%o' \"A\"))
+FROM=$(printf '%o' $((A + number)))
+THRU=$(printf '%o' $((A + number - 1)))
+TRUPPER=$(printf '%b-ZA-%b\n' "\0${FROM}" "\0${THRU}")
+trlower=$(echo $TRUPPER | tr [[:upper:]] [[:lower:]])
+
+exec tr "A-Za-z" "${TRUPPER}${trlower}"