Warning! This version is now obsolete!
Check out the new and improved version (using only Bash built-ins) here!
Here is a command-line (bash) script that uses [cci lang="bash"]sed[/cci] to split the segments of an URI into usable variables. It also validates the given URI since malformed strings produce the text "ERROR" which can be handled accordingly:
[cc lang="bash" nowrap="true"]
# Assembling a sample URI (including an injection attack)
uri_1='http://user:pass@www.example.com:19741/dir1/dir2/file.php'
uri_2='?param=some_value&array[0]=123¶m2=\`cat /etc/passwd\`'
uri_3='#bottom-left'
uri="$uri_1$uri_2$uri_3"
# Parse URI
op=`echo "$uri" | sed -nrf "uri.sed"`
# Handle invalid URI
[[ $op == 'ERROR' ]] && { echo "Invalid URI!"; exit 1; }
# Execute assignments
eval "$op"
# ...work with URI components...
[/cc]Notice the [cci_bash]"uri.sed"[/cci_bash] file given to [cci_bash]sed[/cci_bash]?