[英]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命令中。 非常感謝您的協助。
EDIT1:
我更新了代碼,並將-d選項的{}的引號更改為雙引號,並且相同的錯誤仍然存在。
EDIT2:
使用注釋中提供的代碼更新文件后,我收到:
Warning: You can only select one HTTP request!
當我運行它時。
為了消除我在上面的評論中的任何混亂,應執行以下操作:
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/
您的-d參數周圍有'
單引號,不允許使用變量插值。 使用雙引號"args $id "
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.