我需要看到使用curl或wget找到的搜索,当它找到' 301'状态代码。
这是我使用curl的变量。
website=$(curl -s --head -w %{http_code} https://launchpad.net/~[a-z]/+archive/pipelight -o /dev/null | sed 's#404##g')
echo $website
301
上述方法有效,但只有在网站存在' 301'状态代码。
我想要
echo $website
https://launchpad.net/~mqchael/+archive/pipelight
答案 0 :(得分:0)
您可以添加"有效网址"你的输出。将%{http_code}
更改为"%{http_code} %{url_effective} "
。
从那里开始,这只是一个与正则表达式混淆的问题。将 sed 字符串更改为's#404 [^ ]* ##g'
。这将消除(假设你不知道)不仅仅是404,而且还会吃掉它后面的URL。
所以:
curl -s --head -w "%{http_code} %{url_effective} " https://launchpad.net/~[a-z]/+archive/pipelight -o /dev/null | sed 's#404 [^ ]* ##g''s#404 [^ ]* ##g'
会给你:
301 https://launchpad.net/~j/+archive/pipelight
之后,您可能希望用新行替换HTTP代码。