File tree Expand file tree Collapse file tree 3 files changed +41
-7
lines changed
Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Original file line number Diff line number Diff line change 1- # URL Encode
1+ # URL Encode & Decide
22
3- A simple URL encoder written in bash.
3+ A simple URL encoder and decoder written in bash.
44
5- The scripts reads a single line from stdin and applies urlencoding to it.
5+ The scripts read a single line from stdin and applies URL encoding/decoding to it.
66
7- ## Arguments
7+ ## urlencode arguments
88
99-h : Print help
1010
1111-i : Don't URL encode given characters
1212
13+ ## urldecode arguments
14+
15+ -h : Print help
16+
1317## Example
1418
1519``` console
1620$ urlencode -i ' äö' <<< " pääkkösen paja"
1721pääkkösen%20paja
22+
23+ $ urldecode <<< " p%E4%E4kk%F6sen%20paja"
24+ pääkkösen paja
1825```
Original file line number Diff line number Diff line change 11#! /bin/bash
22
3+ HELP_TEXT=" urldecode [-h]
4+
5+ The script reads a single line from stdin and applies URL decoding to it.
6+
7+ Example:
8+ $ urldecode <<<\" p%E4%E4kk%F6sen%20paja\"
9+ pääkkösen paja
10+ "
11+
312declare hex_regex=' [a-fA-F0-9]{2}'
413
514declare output=" "
615
16+ while getopts ' h' opt; do
17+ case $opt in
18+ h)
19+ echo " ${HELP_TEXT} "
20+ exit 0
21+ ;;
22+ ? )
23+ echo " Unrecognized option: -${OPTARG} " >&2
24+ exit 1
25+ ;;
26+ esac
27+ done
28+
729read -rt .1 str
830
31+ if [ -z " $str " ]; then
32+ echo " Missing stdin, check usage with urldecode -h" >&2
33+ exit 1
34+ fi
35+
936for (( i= 0 ; i < ${# str} ; i++ )) ; do
1037 char=" ${str: i: 1} "
1138 if [ " $char " = " %" ]; then
Original file line number Diff line number Diff line change 22
33HELP_TEXT=" urlencode [-h] [-i chars]
44
5- The scripts reads single line from stdin and applies URL encoding to it.
5+ The script reads a single line from stdin and applies URL encoding to it.
66
77URL encoding can be disabled for select characters using the -i option.
88
@@ -14,7 +14,7 @@ pääkkösen%20paja
1414declare unreserved_chars_regex=' [a-zA-Z0-9\.~_\-]'
1515declare -A ignored
1616
17- while getopts ' : i:h' opt; do
17+ while getopts ' i:h' opt; do
1818 case $opt in
1919 i)
2020 for (( i= 0 ; i < ${# OPTARG} ; i++ )) ; do
@@ -26,7 +26,7 @@ while getopts ':i:h' opt; do
2626 exit 0
2727 ;;
2828 ? )
29- echo Unrecognized option: -${OPTARG} >&2
29+ echo " Unrecognized option: -${OPTARG} " >&2
3030 exit 1
3131 ;;
3232 esac
You can’t perform that action at this time.
0 commit comments