Bash Script_将从读取命令读取的变量插入到命令中
Bash Script_ Inserting variable read from read command into command
我正在使用 BLS 进行项目 API。
参考:http://www.bls.gov/developers/api_unix.htm
我当前的脚本如下:
declare id=""
echo -n "Enter id: "
read id
echo -n "enter startyear: "
read startyear
echo -n "enter endyear: "
read endyear
curl -i -X POST -H 'Content-Type: application/json' -d '{"seriesid": ["$id"], "startyear":'$startyear' , "endyear":'$endyear'}' http://api.bls.gov/publicAPI/v2/timeseries/data/
我已经对其进行了编辑,并像在参考页面上那样手动输入了 ID,并且成功了。我似乎无法从输入中读取 $id 并将其插入到 curl 命令中。非常感谢您对此提供的任何帮助。
编辑1:
我更新了代码并将 -d 选项的 {} 的引号更改为双引号,但同样的错误仍然存在。
编辑2:
使用评论中提供的代码更新文件后,我收到:
Warning: You can only select one HTTP request!
当我运行它。
你的 -d 参数被 '
单引号括起来,不允许变量插值。使用双引号 "args $id "
为了消除我上面评论中的任何混淆,以下内容应该有效:
echo -n "Enter id: "
read id
echo -n "enter startyear: "
read startyear
echo -n "enter endyear: "
read endyear
curl -i \
-X POST \
-H 'Content-Type: application/json' \
-d "{\"seriesid\":[\"${id}\"],\"startyear\":\"${startyear}\",\"endyear\":\"${endyear}\"}" \
http://api.bls.gov/publicAPI/v2/timeseries/data/
我正在使用 BLS 进行项目 API。
参考:http://www.bls.gov/developers/api_unix.htm
我当前的脚本如下:
declare id=""
echo -n "Enter id: "
read id
echo -n "enter startyear: "
read startyear
echo -n "enter endyear: "
read endyear
curl -i -X POST -H 'Content-Type: application/json' -d '{"seriesid": ["$id"], "startyear":'$startyear' , "endyear":'$endyear'}' http://api.bls.gov/publicAPI/v2/timeseries/data/
我已经对其进行了编辑,并像在参考页面上那样手动输入了 ID,并且成功了。我似乎无法从输入中读取 $id 并将其插入到 curl 命令中。非常感谢您对此提供的任何帮助。
编辑1:
我更新了代码并将 -d 选项的 {} 的引号更改为双引号,但同样的错误仍然存在。
编辑2:
使用评论中提供的代码更新文件后,我收到:
Warning: You can only select one HTTP request!
当我运行它。
你的 -d 参数被 '
单引号括起来,不允许变量插值。使用双引号 "args $id "
为了消除我上面评论中的任何混淆,以下内容应该有效:
echo -n "Enter id: "
read id
echo -n "enter startyear: "
read startyear
echo -n "enter endyear: "
read endyear
curl -i \
-X POST \
-H 'Content-Type: application/json' \
-d "{\"seriesid\":[\"${id}\"],\"startyear\":\"${startyear}\",\"endyear\":\"${endyear}\"}" \
http://api.bls.gov/publicAPI/v2/timeseries/data/