3

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
rowoc
  • 239
  • 2
  • 12

3 Answers3

3

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
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
2

There is a cli tool yq to parse yaml files.

The script would look like this:

#!/usr/bin/env bash
name=$(yq '.variables.name' data.yaml)
pass=$(yq '.variables.pass' data.yaml)
count=$(yq '.variables.count' data.yaml)

echo "Details are $name: $pass - Total value $count"

Of course you can also pass the file as argument.

gindex
  • 285
  • 1
  • 11
  • is there any other solution without using any other tool/cli @gindex – rowoc Mar 08 '22 at 11:28
  • @rowoc YAML is complex so there's no general solution other than using a specialised tool for parsing it; that said, if your input is as simple as what you're showing in the question then you can do the job with other (basic/standard) tools – Fravadona Mar 08 '22 at 12:41
  • Yes, there are more options how to parse yaml on [command line](https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script), but yq is most simple one. yq is widely adopted also. Why you cannot use it? – gindex Mar 08 '22 at 12:42
  • @gindex can you recheck your solution , I tried that not working – rowoc Mar 08 '22 at 12:44
  • Should work now. Please check the name of the local file and in the script . – gindex Mar 08 '22 at 12:47
  • Thanks its working, can the code be minimize, I am having more varaibles, for each i have to again write yq '.variables.xxxx' data.yml . Any minification can be done @gindex – rowoc Mar 08 '22 at 12:59
  • I have tried this also but its only taking last 3 variable, if i have more data,its takes last 3 only. . <(sed -nr '/variables:/,$ s/ ([A-Z_]+): (.*)/\1=\2/ p' .data.yml) – rowoc Mar 08 '22 at 13:01
  • @rowoc If you need a solution that will scale smoothly, you might be interested in [this answer](https://stackoverflow.com/questions/71393380/fetch-variables-values-from-yml-and-pass-to-shell-script/71402938#71402938 "support an arbitrary number of variables"). – Cole Tierney Mar 09 '22 at 18:28
1

You can use a single call to yq and put values in an array :

#!/usr/bin/env bash
  
mapfile -t array < <(yq '.variables[]' data.yml)
declare -p array
Philippe
  • 20,025
  • 2
  • 23
  • 32