在bash脚本中使用传递的参数

时间:2019-05-28 18:45:13

标签: bash

我要将两个日期值传递给脚本,并尝试在CURL POST命令中使用它们,如下所示:

starttime=$1
endtime=$2

for apps in $(cat testapps.txt)
do
    curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name": "This is a test","timeRange": {"startTimeMillis":"$1","endTimeMillis":"$2"}, "affects": {"type": "APP"}}'

这给了我500个内部服务器错误。

如果我将$ 1和$ 2的值替换为以下日期/时间,则效果很好。

curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name": "This is a test","timeRange": {"startTimeMillis":"2019-05-28T15:00:00-0400","endTimeMillis":"2019-05-28T16:00:00-0400"}, "affects": {"type": "APP"}}'

我想念什么吗?

1 个答案:

答案 0 :(得分:1)

首先,Dont Read Lines With For

while read apps
do args=( 
    -X POST
    -H "Content-type: application/vnd.appd.cntrl+json;v=1"
    -d '{ "name":"This is a test",
          "timeRange": { 
              "startTimeMillis": "'"$1"'",
              "endTimeMillis": "'"$2"'" }, 
          "affects": {"type": "APP"}}'
   ) 
   # don't you need a URL here?
   curl "${args[@]}" "$apps" # added $apps, assuming that was missing...
done < testapps.txt