-1

I have a sed command that I use to insert a line into a shell script. The name of the script into which I insert varies in every env I'm trying to do this in. This works if I provide the values but when I need to repeat this across a large number of environments I need it to fetch these values from a json file. Any idea how I can achieve this?

Syntax -> sed -i '1 a if ! [ipaddress = <value> && OSTYPE = linux]; then exit(1); endif' qa-<envname>-vm1.sh

sed -i '1 a if ! [ipaddress = 0.0.0.0 && OSTYPE = linux]; then exit(1); endif' qa-QAF-vm1.sh

inserts

if ! [ipaddress = 0.0.0.0 && OSTYPE = linux]; then exit(1); endif after the first line in the file qa-QAF-vm1.sh.

Any ideas how I can fetch the ipaddress value and envname from a json file which lists these values?

  • Possible duplicate of [Unix command-line JSON parser?](http://stackoverflow.com/questions/3858671/unix-command-line-json-parser) – Kev Apr 12 '17 at 08:51

1 Answers1

0

Use jq to query elements from a JSON data structure.

JSON='{"ipaddress": "10.10.10.10", "ostype": "linux"}'

ipaddress=$(jq -r .ipaddress <<<"$JSON")
ostype=$(jq -r .ostype <<<"$JSON")

echo $ipaddress
echo $ostype
ceving
  • 21,900
  • 13
  • 104
  • 178