Skip to content

Instantly share code, notes, and snippets.

@lionello
Last active August 6, 2022 17:37
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 lionello/6bb597fc600932c6c737aab3b402147c to your computer and use it in GitHub Desktop.
Save lionello/6bb597fc600932c6c737aab3b402147c to your computer and use it in GitHub Desktop.
base64url: wrapper script for base64
#!/usr/bin/env bash
# Source: https://gist.github.com/lionello/6bb597fc600932c6c737aab3b402147c
ignore=0
decode=0
dashdash=0
args=()
files=()
while [ $# -gt 0 ]; do
if [ $dashdash -eq 1 ]; then
files+=("$1")
else
case $1 in
-D|-d|--decode) #decode data
decode=1
;;
-i|--ignore-garbage) #when decoding, ignore non-alphabet characters
ignore=1
;;
--help)
base64 --help | sed 's/\(Usage:.base64\)/\1url/'
exit 0
;;
--)
dashdash=1
;;
-*)
args+=("$1")
;;
-|*)
files+=("$1")
;;
esac
fi
shift
done
if [ $decode -eq 0 ]; then
base64 "${args[@]}" -- "${files[@]}" | sed 's!\+!-!g;s!/!_!g;s!=*$!!g'
elif [ $ignore -eq 1 ]; then
sed 's!-!\+!g;s!_!/!g' "${files[@]}" | base64 --decode -i "${args[@]}" 2> /dev/null
else
sed 's!-!\+!g;s!_!/!g' "${files[@]}" | base64 --decode "${args[@]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment