使用 bash 脚本解析 curl 中的变量



嘿,我正在使用管道卷曲方法从帖子中创建任务。当我使用硬编码值从终端运行时,它工作正常。但是当我尝试使用变量执行它时,它会抛出一个错误:

脚本:

#!/bin/bash
echo "$1"
echo "$2"
echo "$3"
echo "$4"
echo "$5"
echo '{
"transactions": [
{
"type": "title",
"value": "$1"
},
{
"type": "description",
"value": "$2"
},
{
"type": "status",
"value": "$3"
},
{
"type": "priority",
"value": "$4"
},
{
"type": "owner",
"value": "$5"
}
]
}' | arc call-conduit --conduit-uri https://mydomain.phacility.com/ --conduit-token mytoken maniphest.edit

执行:

./test.sh "test003 ticket from api post" "for testing" "open" "high" "ahsan"

输出:

test003 ticket from api post
for testing
open
high
ahsan
{"error":"ERR-CONDUIT-CORE","errorMessage":"ERR-CONDUIT-CORE: Validation errors:n  - User "$5" is not a valid user.n  - Task priority "$4" is not a valid task priority. Use a priority keyword to choose a task priority: unbreak, very, high, kinda, triage, normal, low, wish.","response":null}

正如您在错误中看到的那样,它将 $4 和 $5 读取为值而不是变量。而且我不明白如何使用$variables作为这些论点的输入。

您在最后echo周围使用单引号,以便您可以在 JSON 中使用双引号,但这会导致echo打印字符串而不扩展任何内容。您需要对字符串使用双引号,因此您必须转义其中的双引号。

将最后一个echo替换为以下内容:

echo "{
"transactions": [
{
"type": "title",
"value": "$1"
},
{
"type": "description",
"value": "$2"
},
{
"type": "status",
"value": "$3"
},
{
"type": "priority",
"value": "$4"
},
{
"type": "owner",
"value": "$5"
}
]
}"

它会起作用的。为避免此类问题,您可以查看 http://wiki.bash-hackers.org 和 http://mywiki.wooledge.org/BashGuide,以获取针对bash新手的一些一般提示。此外,您可以将shellcheck与许多文本编辑器一起使用,这些编辑器将自动发现此类错误。

最新更新