我正在尝试创建一个小的shell脚本函数,基本上它只返回github仓库的两个最新版本(不包括最新版本)。
get_release() {
curl --silent \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/user/repo/releases |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}
#str="1.1.1 2.2.2 3.3.3 4.4.4 5.5.5 6.6.6 7.7.7 8.8.8 9.9.9"
str=($get_release)
#VERSION=$(get_release)
IFS=', ' read -r -a array <<< "$str"
LASTVERSION=${array[-2]}
PENULTIMATEVERSION=${array[-3]}
echo "${LASTVERSION}"
echo "${PENULTIMATEVERSION}"
但我试着跑的时候却发现了这个:
t.sh: line 17: array: bad array subscript
t.sh: line 18: array: bad array subscript
注意:注解的str变量只是一个数组的模拟,它工作正常,但是当尝试使用get_release函数时,我得到了这个错误。
3条答案
按热度按时间rqcrx0a61#
以下是一个与3.2及更高版本的所有bash版本兼容的工作示例:
...到目前为止正确发出的项目(对于上例中使用的
juji-io/datalevin
项目):cnwbcb6i2#
根据@Philippe的注解,
($get_release)
不会调用您的函数,但$(get_release)
会。根据@Charles Duffy和@glenn jackman对的评论,我更新了代码片段,以便安全地检查和访问数组的最后一个元素。
下面是修改后的代码片段:
下面是另一个基于perl、regex和捕获组的解决方案,即
ghhkc1vu3#
感谢每一个帮助我的人,我能够这样解决它: