在bash shell脚本中如何解析url地址
时间:2019-11-20 08:54:09 来源:igfitidea点击:
在Linux shell中,如何从url字符串中提取域名?
shell中,如何解析url地址?
shell脚本中如何将url中的域名保存到变量中?
从url中获取域名
假设url地址保存在bash shell变量中:
url='https://www.theitroad.com/about-us.html'
可以使用awk获取域名:
echo "$url" | awk -F/ '{print }'
输出示例:
www.theitroad.com
使用sed从URL提取域名
下面是使用sed命令的示例:
url="https://www.theitroad.com/about-us.html" echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||'
在shell脚本中提取域名
在shell脚本中,可以通过shell参数替换获取域名:
# My shell variable
f="https://www.theitroad.local/faq/copy-command/"
## Remove protocol part of url ##
f="${f#http://}"
f="${f#https://}"
f="${f#ftp://}"
f="${f#scp://}"
f="${f#scp://}"
f="${f#sftp://}"
## Remove username and/or username:password part of URL ##
f="${f#*:*@}"
f="${f#*@}"
## Remove rest of urls ##
f=${f%%/*}
## Show domain name only ##
echo "$f"

