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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With