0

so in my code i'm parsing configuration from a file into the script that I now want to execute in one. My first variable is called curl and I now want to include the variable captured in the configuration file but with quotes around some of the parameters. Example below for clarification.

curlsting="curl"

CONFIG=rest.conf
cid=$(awk '/^client_id/{print $3}' "${CONFIG}")
gtype=$(awk '/^grant_type/{print $3}' "${CONFIG}")
csecret=$(awk '/^client_secret/{print $3}' "${CONFIG}")
user=$(awk '/^username/{print $3}' "${CONFIG}")
pw=$(awk '/^password/{print $3}' "${CONFIG}")
ip=$(awk '/^IP/{print $3}' "${CONFIG}")

I'd like the output to be like this

curl '{client_id=test&grant_type="testing"&client_secret=test&username=test&password=test}' http://1.2.3.4:5678/

Basically what is the based way to incorporate 7 variables into one string to execute?

Aaron
  • 65
  • 5
  • With which encoding? URL encoding? JSON encoding? If the password contains `&`, should it become `&`? `%26`? Something else? – Charles Duffy Apr 26 '17 at 15:52
  • using json encoding. Password does not include an &. The & is needed between each parameter and value – Aaron Apr 26 '17 at 15:54
  • That's not valid JSON, though. Valid JSON would be `{"client_id": "test", "grant_type": "testing",` etc. – Charles Duffy Apr 26 '17 at 15:54
  • Obviously, the `&` is a separator -- that's why it's so important to know how to escape it if it shows up in your data (since otherwise, someone providing a username of `myname&mode=evil` could tell your remote system to run in `evil` mode, instead of correctly getting a "no user by that name exists" error). – Charles Duffy Apr 26 '17 at 15:55
  • (On a whole different point, calling `awk` six times is damned silly, instead of just reading everything you want in one pass. For instance, assuming bash 4 or newer: `declare -A vars; while read key _ var _; do vars[$key]=$var; done <"$CONFIG"`, and then you can refer to `${vars[client_id]}`, `${vars[password]}`, etc). – Charles Duffy Apr 26 '17 at 16:01
  • I will take that into consideration. Thanks for your help. I'm very new to scripting which is why i'm asking for advice. – Aaron Apr 26 '17 at 16:05
  • ...btw, if your *real* string form were JSON, then an answer in terms of how to generate contents in that format would typically leverage [`jq`](https://stedolan.github.io/jq/manual/). See for example http://stackoverflow.com/a/43565552/14122 – Charles Duffy Apr 26 '17 at 16:30

0 Answers0