-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.sh
23 lines (21 loc) · 846 Bytes
/
url.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# Given an URL, decompose it into the $URL associative array and return true, or false if URL is invalid.
# Spaces are allowed in the final part, %-encoded characters are not considered.
url()
{
[[ "$*" =~ ^([^:@/\ ]+)://(([^:@/\ ]+)(:([^@/\ ]*))?@)?([^:@/\ ]+)(:([0-9]*))?(/(.*))?$ ]] &&
declare -gA URL=([scheme]=${BASH_REMATCH[1]} [username]=${BASH_REMATCH[3]} [password]=${BASH_REMATCH[5]}
[hostname]=${BASH_REMATCH[6]} [port]=${BASH_REMATCH[8]} [path]=${BASH_REMATCH[10]} )
}
if !(($#)); then
echo "Specify an URL!"
elif url "$*"; then
echo "Scheme = ${URL[scheme]}"
echo "Username = ${URL[username]}"
echo "Password = ${URL[password]}"
echo "Hostname = ${URL[hostname]}"
echo "Port = ${URL[port]}"
echo "Path = ${URL[path]}"
else
echo "URL is invalid"
fi