我正在尝试创建一个bash函数,该函数可以自动更新cli工具。到目前为止,我已经设法得到了这个:
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"
(版本相同(时,它会正确打印它,但当我使用上面提到的流程时就不会了。当我对..._url
和..._version
进行硬编码时,full_url
工作正常。所以我的猜测是,我必须以某种方式捕获输出并将其转换为字符串?或者用另一种方式连接它?
注意:我也使用了..._url=`curl -i ...`
(带有backticks而不是$(...)
(,但这给了我相同的结果。
curl
输出将使用rn
行结尾。url变量中的杂散回车会让您绊倒。用printf '%qn' "$latest_release_url"
观察
试试这个:
latest_release_url=$(
curl --silent -i https://github.com/.../releases/latest
| awk -v RS='rn' '$1 == "location:" {print $2}'
)
然后脚本的其余部分看起来应该是正确的。