Created
May 7, 2015 13:49
-
-
Save prurigro/7283b954b19277c3181e to your computer and use it in GitHub Desktop.
Wrapper to simplify base64 usage
This file contains hidden or 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
#!/usr/bin/env bash | |
script_name=$(egrep -o "[^\/]*$" <<< "$0") | |
function help { | |
printf '%s\n' "Usage:" | |
printf '%s\n' " $script_name e input output → encode to output" | |
printf '%s\n' " $script_name e input → encode to stdout" | |
printf '%s\n' " $script_name d input output → decode to output" | |
printf '%s\n' " $script_name d input → decode to stdout" | |
exit "$1" | |
} | |
function encode { | |
printf '%s' "$(sed ':a;N;$!ba;s|\n||g' <(base64 "$1"))" | |
} | |
function decode { | |
base64 -di < "$1" | |
} | |
if [[ -z "$1" ]]; then | |
help 0 | |
elif [[ -n "$3" ]]; then | |
if [[ -f "$2" ]]; then | |
case $1 in | |
e) | |
encode "$2" > "$3" | |
;; | |
d) | |
decode "$2" > "$3" | |
;; | |
*) | |
help 1 | |
;; | |
esac | |
else | |
printf '%s\n' 'Error: invalid input file' >&2 | |
fi | |
elif [[ -n "$2" ]]; then | |
if [[ -f "$2" ]]; then | |
case $1 in | |
e) | |
encode "$2" | |
;; | |
d) | |
decode "$2" | |
;; | |
*) | |
help 1 | |
;; | |
esac | |
else | |
printf '%s\n' 'Error: invalid input file' >&2 | |
fi | |
else | |
help 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment