Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sed_replace() { echo -n "s#$1#$2#g;";}
- sed_squeeze() { sed_replace "$1+" "${2:-$1}";}
- sed_push() { echo -n 'h;';}
- sed_pop() { echo -n 'g;';}
- sed_print() { echo -n 'p;';}
- sed_script1=$(
- sed_ere_begin() { echo -n "/$1/{"; sed_push;}
- sed_assign() { sed_replace '' "U[$2]='\\$1'";}
- sed_ere_end() { echo '}';}
- sed_ere_begin '^(([^:]+):(\/{0,2}))?(([^:]+):([^@]+)@)?([^:\/]*)?:?([^/]+)?(.*)$'
- sed_assign 1 scheme; sed_print; sed_pop
- sed_assign 5 user; sed_print; sed_pop
- sed_assign 6 pass; sed_print; sed_pop
- sed_assign 7 domain; sed_print; sed_pop
- sed_assign 8 port; sed_print; sed_pop
- sed_assign 9 path; sed_replace '/./' '/'; sed_squeeze '/'; sed_print
- sed_ere_end
- unset -f sed_ere_begin sed_assign sed_ere_end
- )
- url_parse() {
- sed -nre "${sed_script1}"
- }
- url_construct() {
- echo ${U[scheme]:-''}${U[user]}${U[pass]:+':'}${U[pass]}${U[user]:+'@'}${U[domain]}${U[port]:+':'}${U[port]}${U[path]:-'/'}
- }
- declare -A U
- u1='https://UsErNaMe:My5tR0nGp4s5@domain.ru:8088//./dump.txt'
- u2='file:///home/krd/lulz.txt'
- echo "${u1}" | url_parse
- # output:
- # U[scheme]='https://'
- # U[user]='UsErNaMe'
- # U[pass]='My5tR0nGp4s5'
- # U[domain]='domain.ru'
- # U[port]='8088'
- # U[path]='/dump.txt'
- eval $(echo "${u1}" | url_parse)
- url_construct
- # output:
- # https://UsErNaMe:My5tR0nGp4s5@domain.ru:8088/dump.txt
- echo "${u2}" | url_parse
- # output:
- # U[scheme]='file://'
- # U[user]=''
- # U[pass]=''
- # U[domain]=''
- # U[port]=''
- # U[path]='/home/krd/lulz.txt'
- eval $(echo "${u2}" | url_parse)
- url_construct
- # output:
- # file:///home/krd/lulz.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement