首页 > 解决方案 > 在 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"}}'

我错过了什么吗?

标签: bash

解决方案


首先,不要用 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

推荐阅读