echo "${URL}" | awk -F/ '{print $3}' | sed 's/:.*//'
这是针对各种可能性进行测试的要点:https ://gist.github.com/leodotcloud/c4cbedaabeba0a06956188a04022e9ce
样品运行:
# ./get_domain_from_url.sh
For URL:http://1.1.1.1, domain name:1.1.1.1
For URL:http://1.1.1.1:8443, domain name:1.1.1.1
For URL:https://1.1.1.1, domain name:1.1.1.1
For URL:https://1.1.1.1:8443, domain name:1.1.1.1
For URL:http://hello-world, domain name:hello-world
For URL:http://hello-world.com, domain name:hello-world.com
For URL:http://sub.hello-world.com, domain name:sub.hello-world.com
For URL:http://hello-world:8080, domain name:hello-world
For URL:http://hello-world.com:8080, domain name:hello-world.com
For URL:http://sub.hello-world.com:8080, domain name:sub.hello-world.com
For URL:https://hello-word/foo/bar, domain name:hello-word