4

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
2
  • 2
    Why is this tagged as bash and powershell? Commented Mar 8, 2022 at 10:52
  • 1
    @phd i have updated the tags remove the downvote. Thanks Commented Mar 8, 2022 at 14:04

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

10 Comments

it gives me "Error: bad expression, please check expression syntax" for the yq command
I've upgraded to yq 4.29.2 and now I'm seeing Error: bad expression, please check expression syntax. I'll need to change the yq expression for version 4 and above.
Thanks. I tried to play around with it but could not fix. If you find a solution please update the answer for new versions.
Added support for yq 4 and above. The yq command is a lot cleaner now. Thanks for your report! Please let me know if this works for you.
nice. that worked
|
3

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.

8 Comments

is there any other solution without using any other tool/cli @gindex
@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
Yes, there are more options how to parse yaml on command line, but yq is most simple one. yq is widely adopted also. Why you cannot use it?
@gindex can you recheck your solution , I tried that not working
Should work now. Please check the name of the local file and in the script .
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.