【发布时间】:2018-05-20 15:31:16
【问题描述】:
我有一个脚本可以帮助我将一些数据发布到系统
我有一个功能
function curler(){
#curl "https://secure.aha.io/api/v1/features/"$featureID"/requirements?access_token={myAccessToken}" -d "$minReq" -X POST -H "Content-type: Application/json" -H "Accept: application/json"
echo "https://secure.aha.io/api/v1/features/"$1"/requirements?access_token={myAccessToken}" -d "$2" -X POST -H "Content-type: Application/json" -H "Accept: application/json"
echo $1
echo $2
}
然后我调用传入$featureID 的函数和一个minReq 我用我的json 有效负载定义的变量
minReq='{"requirement":{"name":"'$elementName' - Min
Length","workflow_status":{"name":"Defined"},"description":"-
Indicates the minimum length of strings or
numbers.","assigned_to_user":{"email":"kelly@someEmail.com"}}}'
在curler 函数中,您可以看到我已经尝试通过实际变量名和$1 和f $2 基于arg 索引进行引用
最终我想使用 curl,但为了测试我有 echo
我用curler $featureID $minReq 调用curler 函数,其中featureID 是字符串MDL-123 和$minReq 包含我的json 有效负载
echo 输出以下内容
https://secure.aha.io/api/v1/features/mdl-149/requirements?access_token={myAccessToken} -d {"requirement":{"name":"Act-On -X POST -H Content-type: Application/json -H Accept: application/json
mdl-149
{"requirement":{"name":"Act-On
elements-mac27:脚本 kellygold$
我看到的是当我将$minReq 参数引用为$2 时插入$elementName 后,我的有效负载$minReq 被切断
在我上面的例子中 elementName 是'act-on' $elementName 被定义并且之前是从用户那里收集的
我还注意到,如果我将变量作为其原生名称 $minReq 引用,那么它会按预期工作
所以下面的回声
echo "https://secure.aha.io/api/v1/features/"$1"/requirements?access_token={myaccesstoken}" -d "$minReq" -X POST -H "Content-type: Application/json" -H "Accept: application/json"
预期输出
https://secure.aha.io/api/v1/features/mdl-149/requirements?access_token={myAccessToken} -d {"requirement":{"name":"act-on - Min Length","workflow_status":{"name":"Defined"},"description":"- Indicatest the minimum length of strings or numbers.","assigned_to_user":{"email":"kelly@someEmail.com"}}} -X POST -H Content-type: Application/json -H Accept: application/json
mdl-149
{"requirement":{"name":"act-on
elements-mac27:Scripts kellygold$
为什么当我通过函数调用$minReq 中传递的变量名引用我的参数时,它按预期工作,但是当我传递$2 时,它会在变量插入我的有效负载后立即中断?如何在维护数据的同时引用 arg 编号而不是变量名称?
【问题讨论】:
-
发生这种情况是因为您没有引用变量。您必须致电
curler "$featureID" "$minReq"并确保对所有其他扩展进行正确引用。在你的完整脚本上尝试ShellCheck。
标签: json bash unix curl command-line-arguments