【发布时间】:2019-09-23 09:28:41
【问题描述】:
当我尝试执行以下脚本并收到错误消息时:“curl: (3) URL using bad/illegal format or missing URL”
#!/bin/bash
stage="develop"
branch="branch_name"
getDefinition=$(curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions?api-version=5.1")
for def in $(echo "$getDefinition" | jq '.value[] | select (.path=="\\Some_path\\'$stage'") | .id'); do
getBuildInfo=$(curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1")
# echo $def
body=$(echo "${getBuildInfo}" | jq '.repository.defaultBranch = "refs/heads/release/'"${branch}"'"' | jq '.options[].inputs.branchFilters = "[\"+refs/heads/release/'"${branch}"'\"]"' | jq '.triggers[].branchFilters[] = "+refs/heads/release/'"${branch}"'"')
echo ${body} > data.json
done
当我尝试将变量 ${def} 传递到一行时会发生这种情况:
curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"
但是当我声明一个数组时,curl 按预期工作。 示例:
declare -a def
def=(1 2 3 4)
curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"
您能否建议我如何正确地将变量传递到 URL 中?
【问题讨论】:
-
尝试 echo "-"$def"-" 看看是否有不需要的前导或结尾字符
-
这可能不会显示非打印字符,
echo可能会将“-”误认为是选项分隔符。我会推荐echo "'$def'" | LC_ALL=C cat -vt。或者在问题部分之前使用set -x,这样shell 会在执行命令时打印命令(可能会在任何有趣的字符周围加上奇怪的引用/转义)。 -
非常感谢@franzisk,@GordonDavisson!变量 $def 包含 ^M 字符。删除此 CURL 后工作正常。