Created
February 3, 2017 02:27
-
-
Save joshisa/297b0bc1ec0dcdda0d1625029711fa24 to your computer and use it in GitHub Desktop.
Parsing of URLs using bash sh scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Referenced and tweaked from http://stackoverflow.com/questions/6174220/parse-url-in-shell-script#6174447 | |
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" | |
# remove the protocol | |
url="$(echo ${1/$proto/})" | |
# extract the user (if any) | |
userpass="$(echo $url | grep @ | cut -d@ -f1)" | |
pass="$(echo $userpass | grep : | cut -d: -f2)" | |
if [ -n "$pass" ]; then | |
user="$(echo $userpass | grep : | cut -d: -f1)" | |
else | |
user=$userpass | |
fi | |
# extract the host | |
host="$(echo ${url/$user@/} | cut -d/ -f1)" | |
# by request - try to extract the port | |
port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')" | |
# extract the path (if any) | |
path="$(echo $url | grep / | cut -d/ -f2-)" | |
echo "url: $url" | |
echo " proto: $proto" | |
echo " user: $user" | |
echo " pass: $pass" | |
echo " host: $host" | |
echo " port: $port" | |
echo " path: $path" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated Version (2021-01-21T19:39:05Z):
@
in the username or password)Example: