Skip to content

Commit 36c3772

Browse files
committed
Add help and usage to urldecode
1 parent 1510f77 commit 36c3772

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
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"
1721
pääkkösen%20paja
22+
23+
$ urldecode <<<"p%E4%E4kk%F6sen%20paja"
24+
pääkkösen paja
1825
```

urldecode

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
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+
312
declare hex_regex='[a-fA-F0-9]{2}'
413

514
declare 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+
729
read -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+
936
for ((i=0; i < ${#str}; i++)); do
1037
char="${str:i:1}"
1138
if [ "$char" = "%" ]; then

urlencode

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
HELP_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
77
URL encoding can be disabled for select characters using the -i option.
88
@@ -14,7 +14,7 @@ pääkkösen%20paja
1414
declare unreserved_chars_regex='[a-zA-Z0-9\.~_\-]'
1515
declare -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

0 commit comments

Comments
 (0)