bash - 在 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"}}'
我错过了什么吗?
解决方案
首先,不要用 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
推荐阅读
- mysql - MySQL 查询后的重复行
- python - 没有STDOUT的两个python脚本之间的进程间通信
- bash - bash 中的条件错误处理取决于退出代码
- javascript - 当有人在另一个字段中输入文本时如何将隐藏字段更改为真
- count - 内连接 + 计数 + 在 Psequel 中的位置
- tensorflow - 根据值拆分张量
- swift - UILabel 上的外发光效果
- javascript - 将 id 添加到 Google 可视化表
- spring - spring 文档所需的 Spring 传播
- java - 创建 koloboke 地图时出现异常。(内部运行时错误)java.util.NoSuchElementException