Reference Json as $arg bash with variables in payload (arg1 arg2 not variable passed)
Reference Json as $arg bash with variables in payload (arg1 arg2 not variable passed)
我有一个脚本可以帮助我POST一些数据到系统
我有一个功能
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/""/requirements?access_token={myAccessToken}" -d "" -X POST -H "Content-type: Application/json" -H "Accept: application/json"
echo
echo
}
然后我调用传入 $featureID
的函数和我用 json 有效负载
定义的 minReq
变量
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
函数中,您可以看到我已尝试通过实际变量名称和 </code> 以及基于 arg index</p> 的 <code>
进行引用
最终我想使用 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
参数引用为 </code></p> 时,我的负载 <code>$minReq
在插入 $elementName
后被截断
在我上面的示例中,elementName 是 'act-on' $elementName 已定义并且之前是从用户那里收集的
我还注意到,如果我引用变量作为它的本机名称 $minReq
那么它会按预期工作
所以下面的回显
echo "https://secure.aha.io/api/v1/features/""/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
中传递的变量名引用我的参数时它按预期工作,但是当我传递
时它在变量是插入我的有效载荷?如何在维护数据的同时引用 arg 编号而不是变量名?
您需要使用双引号括起来的参数调用您的函数:
curler "$featureID" "$minReq"
没有双引号,
shell 执行分词,
而不是传递两个参数,
该函数将通过多个参数接收它们。
根据经验,
您应该在命令参数中使用双引号变量。
我有一个脚本可以帮助我POST一些数据到系统
我有一个功能
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/""/requirements?access_token={myAccessToken}" -d "" -X POST -H "Content-type: Application/json" -H "Accept: application/json"
echo
echo
}
然后我调用传入 $featureID
的函数和我用 json 有效负载
minReq
变量
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
函数中,您可以看到我已尝试通过实际变量名称和 </code> 以及基于 arg index</p> 的 <code>
进行引用
最终我想使用 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
参数引用为 </code></p> 时,我的负载 <code>$minReq
在插入 $elementName
后被截断
在我上面的示例中,elementName 是 'act-on' $elementName 已定义并且之前是从用户那里收集的
我还注意到,如果我引用变量作为它的本机名称 $minReq
那么它会按预期工作
所以下面的回显
echo "https://secure.aha.io/api/v1/features/""/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
中传递的变量名引用我的参数时它按预期工作,但是当我传递 时它在变量是插入我的有效载荷?如何在维护数据的同时引用 arg 编号而不是变量名?
您需要使用双引号括起来的参数调用您的函数:
curler "$featureID" "$minReq"
没有双引号, shell 执行分词, 而不是传递两个参数, 该函数将通过多个参数接收它们。
根据经验, 您应该在命令参数中使用双引号变量。