我有一個執行 302 重定向鏈的測驗站點。我正在尋找撰寫一個 BASH 腳本,該腳本將從鏈中每個頁面的回應標頭中列印重定向 URL。
以下命令將從單個回應標頭中去除 URL 并列印它。
$ curl -Is http://iperformalotofredirects.com/ | sed -ne 's/Location: //' -e6p
http://iperformalotofredirects.com/firststep/
$ curl -Is http://iperformalotofredirects.com/firststep/ | sed -ne 's/Location: //' -e6p
http://iperformalotofredirects.com/nextstep/
我寫了一個運行正常的 python 腳本,但為了研究,我也試圖在 BASH 腳本中做同樣的事情。
#!/bin/bash
geturl() {
if [[ $1 == http* ]]
then
echo $1
geturl $(curl -Is $1 | sed -ne 's/Location: //' -e6p)
fi
}
geturl http://iperformalotofredirects.com/
第一次迭代后腳本變為空
$ ./curlit.sh
http://iperformalotofredirects.com/
http://iperformalotofredirects.com/firststep/
我需要的是讓腳本繼續直到筋疲力盡。我的手動結果表明,更多的東西之后出現,http://iperformalotofredirects.com/firststep/而我的腳本未能實作這一點。
編輯: 包括@konsolebox 的答案作為參考
#!/bin/bash
main() {
local location=$1
while [[ $location == http* ]]; do
echo "$location"
location=$(curl -Is "$location" | awk '/Location: / { print $2 }')
done
}
main "$@"
以相同的方式復制并粘貼此腳本。運行它:
$ ./curlit.sh http://iperformalotofredirects.com/
http://iperformalotofredirects.com/
http://iperformalotofredirects.com/firststep/
我希望看到的:
$ ./curlit.sh http://iperformalotofredirects.com/
http://iperformalotofredirects.com/
http://iperformalotofredirects.com/firststep/
http://iperformalotofredirects.com/nextstep/
http://iperformalotofredirects.com/anotherstep/
http://iperformalotofredirects.com/moresteps/
http://iperformalotofredirects.com/finalstep/
編輯: fravadona 請求了整個標題
$ curl -Is http://iperformalotofredirects.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.18.0
Date: Fri, 07 Jan 2022 09:20:56 GMT
Content-Type: text/html
Content-Length: 145
Location: http://iperformalotofredirects.com/firststep/
Connection: keep-alive
$ curl -Is http://iperformalotofredirects.com/firststep/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.18.0
Date: Fri, 07 Jan 2022 09:20:56 GMT
Content-Type: text/html
Content-Length: 145
Location: http://iperformalotofredirects.com/nextstep/
Connection: keep-alive
uj5u.com熱心網友回復:
試試這個代碼進行除錯:
#!/bin/bash
main() {
local location=$1
while [[ $location == http* ]]; do
printf '%q\n' "$location"
location=$(curl -Is "$location" | awk '/^Location: / { print $2 }')
done
}
main "$@"
有了這個,我發現運行main https://kernel.org輸出
$'https://www.kernel.org/\r'這意味著在位置的末尾可能有一個回車,這顯然是問題的可能原因。修復很簡單:
#!/bin/bash
main() {
local location=$1
while [[ $location == http* ]]; do
printf '%q\n' "$location"
location=$(curl -Is "$location" | awk '/^Location: / { print $2 }')
location=${location%$'\r'}
done
}
main "$@"
uj5u.com熱心網友回復:
這有效但沒有遞回:
#!/bin/bash
main() {
local location=$1
while [[ $location == http* ]]; do
echo "$location"
location=$(curl -Is "$location" | awk '/Location: / { print $2 }')
done
}
main "$@"
您可以通過將location=$(curl ...行更改為以下內容來檢查收到的回應:
response=$(curl -Is "$location")
printf "---- Response ----\n%s\n------------------\n" "$response"
location=$(awk '/Location: / { print $2 }' <<< "$response")
echo "Found location: $location"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/404397.html
標籤:
上一篇:[Azure DevOps] 管理測驗計劃、測驗套件和測驗用例
下一篇:帶反斜杠的Heredoc
