Home > OS >  Fetch variables values from yml and pass to shell script?
Fetch variables values from yml and pass to shell script?

Time:03-08

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

CodePudding user response:

There is a cli tool yq to parse yaml files.

The script would look like this:

#!/bin/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.

CodePudding user response:

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
  • Related