2012-12-17 43 views
3

您好我想在shell腳本中使用curl,如下所示,但我無法用CURL中的變量$ line替換。請建議在shell腳本中使用curl來代替變量

while read line 
do 
    echo "allowing mac $line" 
    curl -X POST -d '{"src-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json 
    curl -X POST -d '{"dst-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json 
done < /home/floodlight/allowedmacs 

回答

7

在單引號中沒有變量替換。切換到一倍左右的擴張,像這樣:

curl -X POST -d '{"src-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json 
curl -X POST -d '{"dst-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json 

或者你可以使用雙引號將整個事情,逃離那些內:

curl -X POST -d "{\"src-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json 
curl -X POST -d "{\"dst-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json 

無論哪種方式,不能在裏面單如果您希望擴展,請參閱$line

+0

或交換雙/單引號:''{'src-mac':'$ line'}「' –

+1

@glennjackman - nope。雖然在JavaScript代碼中有效的文字,這是不合法的[JSON](http://www.json.org/),它需要雙引號字符串。 –