Skip to content

Instantly share code, notes, and snippets.

@meleu
Last active April 5, 2020 14:28
Show Gist options
  • Save meleu/2290a2fcc9bf979d99b1003bb05c72f8 to your computer and use it in GitHub Desktop.
Save meleu/2290a2fcc9bf979d99b1003bb05c72f8 to your computer and use it in GitHub Desktop.
urldecode the contents of a file and print it in standard output.
#!/usr/bin/env bash
urldecode() {
local encoded="${*//+/ }"
printf '%b\n' "${encoded//%/\\x}"
}
main() {
local file="$1"
local line
if [[ -z "$file" ]]; then
file='/dev/stdin'
echo "WARNING: no filename given, reading from stdin." >&2
elif [[ ! -f "$file" ]]; then
echo "ERROR: invalid file '$file'" >&2
echo "USAGE: $0 [filename]" >&2
exit 1
fi
while IFS= read -r line || [[ -n "$line" ]]; do
urldecode "$line"
done < "$file"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment