Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch variables values from yml and pass to shell script?

Tags:

bash

yaml

data.yml

variables:
  count: "100"
  name: "sss"
  pass: "123"

file.sh

#!/bin/bash
echo "Details are "$name":"$pass" - Total value "$count"

I need to fetch the variable values from data.yml and pass to file.sh

calling file.sh should give Output:

Details are sss:123 - Total value 100
like image 515
rowoc Avatar asked Sep 02 '25 03:09

rowoc


1 Answers

Read name value pairs from yq and use bash printf -v to initialize variables. A little over the top, but a nice way to initialize a script. Note that adding variables does not change the while loop.

file.sh

#!/bin/bash

while read -r key val; do
    printf -v "$key" "$val"
done < <(yq '.variables[] | key + " " + .' data.yml)

echo "Details are $name: $pass - Total value $count"
echo 'Some extra variables:'
echo "\$address: $address, \$phone: $phone, \$url: $url"

data.yml

variables:
  count: "100"
  name: "sss"
  pass: "123"
  address: "42 Terrapin Station"
  phone: "999-999-9999"
  url: "http://www.example.com"

output

Details are sss: 123 - Total value 100
Some extra variables:
$address: 42 Terrapin Station, $phone: 999-999-9999, $url: http://www.example.com
like image 68
Cole Tierney Avatar answered Sep 05 '25 01:09

Cole Tierney