我正在嘗試創建一個自動更新 cli 工具的 bash 函式。到目前為止,我已經設法得到這個:
update_cli_tool () {
# the following will automatically be redirected to .../releases/tag/vX.X.X
# there I get the location from the header, and remove it to get the full url
latest_release_url=$(curl -i https://github.com/.../releases/latest | grep location: | awk -F 'location: ' '{print $2}')
# to get the version, I get the 8th element from the url .../releases/tag/vX.X.X
latest_release_version=$(echo "$latest_release_url" | awk -F '/' '{print 8}')
# this is where it breaks
# the first part just replaces the "tag" with "download" in the url
full_url="${latest_release_url/tag/download}/.../${latest_release_version}.zip"
echo "$full_url" # or curl $full_url, also fails
}
預期輸出:https://github.com/.../download/vX.X.X/vX.X.X.zip
實際輸出:-.zip-.../.../releases/download/vX.X.X
當我只是echo "latest_release_url: $latest_release_url"(版本相同)時,它會正確列印它,但當我使用上述流程時卻不是。當我對..._urland進行硬編碼時..._version,full_url作業正常。所以我的猜測是我必須以某種方式捕獲輸出并將其轉換為字串?或者也許以另一種方式連接它?
注意:我也使用過..._url=`curl -i ...` (用反引號代替$(...)),但這給了我相同的結果。
uj5u.com熱心網友回復:
curl輸出將使用行\r\n尾。url 變數中的雜散回車會讓你絆倒。觀察它printf '%q\n' "$latest_release_url"
嘗試這個:
latest_release_url=$(
curl --silent -i https://github.com/.../releases/latest \
| awk -v RS='\r\n' '$1 == "location:" {print $2}'
)
然后腳本的其余部分應該看起來正確。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/516885.html
標籤:重击壳脚本
