Last active
May 12, 2017 11:33
-
-
Save kamontat/744672962b614a35a71d73bd8bc6664a to your computer and use it in GitHub Desktop.
encode and decode str with base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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