[英]How to pass a variable to a curl command in a bash script?
I am using the bash code below to store the result of a curl command in a text file.我正在使用下面的 bash 代码将 curl 命令的结果存储在文本文件中。
cat /c/customer_files/Bain/artifacts1.txt
sample=$(curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer <my_token>' '<api_>' | jq -r '.items[] | .id')
echo "$sample" >> /c/my_files/artifacts1.txt
This generates a text file with content below:这会生成一个包含以下内容的文本文件:
606b69cff140fe0d98e78d2a
60a40910c403d464225343b5
607f1e14d514043adcf4a0f6
60c36c380093aa519b816554
Now, I want to iterate through this file line by line.现在,我想逐行遍历这个文件。 I am using the code below to do that.
我正在使用下面的代码来做到这一点。
while read -r line; do
#reading each line
echo "Line No. $n : $line";
n=$((n+1))
This is producing correct result as expected.这正如预期的那样产生正确的结果。
Line No. 1 : 606b69cff140fe0d98e78d2a
Line No. 2 : 60a40910c403d464225343b5
Line No. 3 : 607f1e14d514043adcf4a0f6
Line No. 4 : 60c36c380093aa519b816554
I want to pass variable $line to CURL command via json body below.我想通过下面的 json 主体将变量 $line 传递给 CURL 命令。
input_json="{"executable": "<some ID>", "keepTargetResources": true,"keepTargetRunProfiles": true,"advanced": {"artifactId": "$line","artifactType": "ACTION"}}"
However, this produces a result below:但是,这会产生以下结果:
,artifactType: ACTION}}d6a50bb75bbe81, keepTargetResources: true,keepTargetRunProfiles:
true,advanced: {artifactId: 606b69cff140fe0d98e78d2a
,artifactType: ACTION}}d6a50bb75bbe81, keepTargetResources: true,keepTargetRunProfiles:
true,advanced: {artifactId: 60a40910c403d464225343b5
,artifactType: ACTION}}d6a50bb75bbe81, keepTargetResources: true,keepTargetRunProfiles:
true,advanced: {artifactId: 607f1e14d514043adcf4a0f6
{executable: 60ca3bf02ed6a50bb75bbe81, keepTargetResources: true,keepTargetRunProfiles:
true,advanced: {artifactId: 60c36c380093aa519b816554,artifactType: ACTION}}
It creates an output in the desired format only for the last record:它仅为最后一条记录创建所需格式的输出:
{executable: 60ca3bf02ed6a50bb75bbe81, keepTargetResources:
true,keepTargetRunProfiles:true,advanced: {artifactId:
60c36c380093aa519b816554,artifactType: ACTION}}
For 1st 3 records, it looks like it is overwriting content at the start of the line.对于 1st 3 记录,它看起来像是覆盖了行首的内容。
What am I doing wrong?我究竟做错了什么? Please advice.
请指教。
Thanks in Advance.提前致谢。
JSON needs to have its variables and values surrounded with "
. Use \\
to prevent the shell to interpret the "
. JSON 需要将其变量和值用
"
括起来。使用\\
以防止 shell 解释"
。
The extra carriage return is a mystery... Maybe an extra clean up of line
could make it额外的回车是一个谜......也许额外的
line
清理可以使它
Prefer printf
to echo
and use "${line}"
for more safety.更喜欢
printf
echo
并使用"${line}"
以提高安全性。
Give this a try:试试这个:
artifact_id="$(printf "%s" "${line}" | sed 's/^\(.*[^[:blank:]]\)[[:blank:]]*$/\1/g')"
input_json="{\"executable\": \"<some ID>\", \"keepTargetResources\": true,\"keepTargetRunProfiles\": true,\"advanced\": {\"artifactId\": \"${artifact_id}\",\"artifactType\": \"ACTION\"}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.