Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active May 12, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/744672962b614a35a71d73bd8bc6664a to your computer and use it in GitHub Desktop.
Save kamontat/744672962b614a35a71d73bd8bc6664a to your computer and use it in GitHub Desktop.
encode and decode str with base64
#!/bin/bash
INPUT=false
ACTION=""
CODE=""
while getopts 'iD:E:h' flag; do
case "${flag}" in
i) INPUT=true ;;
D) ACTION="D" && CODE="$OPTARG" ;;
E) ACTION="E" && CODE="$OPTARG" ;;
h) printf "available option \n\tD<STR> - decode str\n\tE<STR> - encode str\n\ti - decode/encode by reading input separately" && exit 0 ;;
*) error "Unexpected option ${flag} run -h for more information" ;;
esac
done
# INPUT SECTION
if $INPUT; then
while true; do
printf "\nInput str[e<STR>|d<STR>|q]: \n"
read str
if [[ $str == "q" ]]; then
exit
fi
first="${str:0:1}"
real_str="${str#?}"
if [[ $first == "d" ]]; then
echo "$real_str" | base64 -D
else
echo "$real_str" | base64
fi
done
fi
# NORMAL SECTION
if [[ $CODE == "" ]]; then
echo "more information at -h"
exit 1
fi
if [[ $ACTION == "D" ]]; then
echo "$CODE" | base64 -D
else
echo "$CODE" | base64
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment