Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use curl in shell script to substitue a variable

Tags:

shell

curl

Hi i am trying to use curl in shell script as shown below but i am not able to substitute the variable $line in the CURL . please suggest

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
like image 578
user1459427 Avatar asked Mar 28 '26 05:03

user1459427


1 Answers

No variable substitution in single quotes. Switch to double around the expansion, like this:

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

Or you could use double-quotes around the whole thing and escape the inner ones:

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

Either way, can't be inside single quotes when you get to $line if you want it expanded.

like image 152
Mark Reed Avatar answered Mar 29 '26 20:03

Mark Reed