wiki/_next/data/Ie9t5zutrXP6Of75Cb5xF/cookbook.bash.percent-encoding.json
2022-07-29 15:41:17 +00:00

1 line
3.3 KiB
JSON
Raw Permalink Blame History

{"pageProps":{"metadata":{"date":"\"2021-06-23 21:10:33 +08:00\"","date_modified":"\"2022-04-16 20:17:53 +08:00\"","language":"en","source":""},"title":"Percent encoding in Bash","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Percent-encoding is commonly used in URLs.\nPretty nice especially when used with spaces.\nThough, this is something that is rarely needed in "},{"type":"element","tagName":"a","properties":{"href":"id:dd9d3ffa-03ff-42a1-8c5d-55dc9fcc70fe"},"children":[{"type":"text","value":"GNU Bash"}]},{"type":"text","value":" except if you're writing a quick script.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Based from "},{"type":"element","tagName":"a","properties":{"href":"https://gist.github.com/cdown/1163649"},"children":[{"type":"text","value":"this implementation"}]},{"type":"text","value":".\nIt was modified to be closer to the output from "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3.8/library/urllib.parse.html#urllib.parse.urlencode"},"children":[{"type":"text","value":"Python "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"urlencode"}]}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"function urlencode {\n local msg=$1\n local length=\"${#1}\"\n\n for (( i = 0; i < length; i++ )); do\n local ch=\"${msg:i:1}\"\n case $ch in\n [a-zA-Z0-9.~_-])\n printf \"%c\" $ch;;\n*)\n printf '%s' \"$ch\" | xxd -plain -cols 1 | {\n while read hex; do\n printf \"%%%s\" $hex\n done\n };;\n esac\n done\n printf \"\\n\"\n}\n\nurlencode \"El Doggo\"\nurlencode \"El Niño\"\nurlencode \"Whoa there, sonny!\"\n"}]}]},{"type":"element","tagName":"pre","properties":{"className":["fixed-width"]},"children":[{"type":"text","value":"ElDoggo\nElNi<4E>o\nWhoatheresonny"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\nTo decode percent-encoded strings:\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"function urldecode {\n # urldecode <string>\n\n # Replace all pluses with spaces since it usually represents it in URLs.\n local url_encoded=\"${1//+/ }\"\n\n # Replace all percent sign with '\\x' and print the message with byte interpretation.\n printf '%b\\n' \"${url_encoded//%/\\\\x}\"\n}\n\nurldecode \"El%20Doggo\"\nurldecode \"%e9%bd%8b%e8%97%a4%e3%83%bbB%e3%83%bb%e6%a5%b5%e3%83%bb%e5%b0%86%e5%97%a3\"\n"}]}]},{"type":"element","tagName":"pre","properties":{"className":["fixed-width"]},"children":[{"type":"text","value":"El Doggo\n齋藤・ B ・極・将嗣"}]}]},"backlinks":[]},"__N_SSG":true}