我有循环发送 curl 请求的 bash 脚本。第一次迭代工作正常,但第二次有像
curl: (6) Could not resolve host:key1, Error: Process completed with exit code 6.
这样的错误
我的代码看起来像
IFS=',' read -ra keys <<< "key1, key2, key3, key4, key5"
for key in "${keys[@]}"
do
echo "Deleting cache for key: $key"
response=$(curl -X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer GITHUB_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPOSITORY/actions/caches?key=$key)
if [ $? -ne 0 ]; then
echo "Error occurred while deleting key $key"
continue
fi
echo "Response for key $key: $response"
done
我该如何解决这个问题
你声明数组的方式有误,检查:
IFS=',' read -ra keys <<< "key1, key2, key3, key4, key5"
echo "[${keys[1]}]"
[ key2]
您的钥匙中有前导不需要的空间
您的列表/数组在每个 KEY 前面引入一个空格....尝试添加:
IFS=',' read -ra keys <<< "key1, key2, key3, key4, key5"
for key in "${keys[@]}"
do
key=$(echo -n "$key"|xargs) ## <---- cleanup spaces
echo "Deleting cache for key: $key"
response=$(curl -X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer GITHUB_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPOSITORY/actions/caches?key=$key)
if [ $? -ne 0 ]; then
echo "Error occurred while deleting key $key"
continue
fi
echo "Response for key $key: $response"
done