简体   繁体   English

在curl URL Shell脚本中分配变量

[英]Assign a variable in curl URL shell script

I am trying to loop through the cells in a column in a csv file and assign each cell as a variable. 我试图遍历csv文件中列的单元格,并将每个单元格分配为一个变量。 I then want to take that variable and put it in a URL so it puts the cell from the csv in the url. 然后,我想将该变量放入URL中,以便将csv中的单元格放入URL中。 When I run the script, it gives the error: "curl: (3) Illegal characters found in URL" 当我运行脚本时,它给出错误:“ curl:(3)URL中发现非法字符”

#!/bin/bash

IFS=,

cat /Users/User/Desktop/hardwares_2018-01-23_11-02-05.csv | while read id



temp=`echo $id | sed 's/"//g'`
#echo $temp

do

curl -H "X-Application-Authorization: Bearer Token" -H 'Accept: application/vnd.application.v2.1+xml' -X DELETE https://api.application.com/hardwares/${temp}.xml



done

My csv file looks like this 我的csv文件如下所示

1945603
1945604
1945605
1945606
1945607
1945608
1945609
1945610
1945611
1945612

Any ideas? 有任何想法吗?

Thanks 谢谢

我认为您想要在“同时读取ID”部分之后的“执行”指令,而不是现在的位置。

I believe the problem is in the first headr -H flag. 我相信问题出在第一个标题-H标志中。 It has white space in the string which might be getting interpreted as another argument to curl . 它在字符串中有空格,这可能会被解释为curl另一个参数。

Here I made some generic modification to the script. 在这里,我对该脚本进行了一些常规修改。 I don't have an environment to test it. 我没有测试环境。 Hope it helps. 希望能帮助到你。

$ cat test.sh
#!/bin/bash

h1flag="'X-Application-Authorization: Bearer Token'"
h2flag="'Accept: application/vnd.application.v2.1+xml'"
reqflag="DELETE"
baseurl="https://api.application.com/hardwares"
while read -r id
do
    curl --verbose -H "${h1flag}" -H "${h2flag}" -X "${reqflag}" "${baseurl}/$id" 
done < input.csv

An example line from above would look like the following, 上面的示例行如下所示,

$ curl --verbose \
  -H 'X-Application-Authorization: Bearer Token' \
  -H 'Accept: application/vnd.application.v2.1+xml' \
  -X DELETE https://api.application.com/hardwares/1945603

I would suggest to use this on the cmdline first before running it using the script. 我建议在使用脚本运行它之前先在cmdline上使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM