Home > Software design >  Passing bash script variables to .yml file for use as child and subdirectories
Passing bash script variables to .yml file for use as child and subdirectories

Time:04-26

I'm very new to bash scripting so please link me to places if I'm asking silly questions here!

I'm trying to write a bash script test-bash.sh that will pass arguments from a txt file test-text.txt to a .yml file test-yaml.yml, to be used as child and subdirectory arguments (by that I mean e.g. /path/to/XXXX/child/ and /path/to/XXXX where XXXX is the argument passed from the .sh script.

I'm really struggling to wrap my head around a way to do this so here is a small example of what I'm trying to achieve:

test-text.txt:

folder1
folder2

test-yaml.yml:

general:
  XXXX: argument_from_bash_script
  rawdatadir: '/some/data/directory/XXXX'
  input: '/my/input/directory/XXXX/input'
  output: '/my/output/directory/XXXX/output'

test-bash.sh:

#!/bin/bash

FILENAME="test-text.txt"    
FOLDERS=$(cat $FILENAME)

for FOLDER in $FOLDERS
do
        ~ pass FOLDER to .yml file as argument ~
        ~ run stuff using edited .yml file ~
done

Where the code enclosed in '~' symbols is pseudo code.


I've found this page on using export, would this work in the looping case above or am I barking up the wrong tree with this?

Many thanks in advance for any help on this, I really appreciate it.

CodePudding user response:

Does envsubst solve your problem?

For example, if I have a test-yaml.yml that contains $foo:

cat test-yaml.yml 

output:

general:
  $foo: argument_from_bash_script
  rawdatadir: '/some/data/directory/$foo'
  input: '/my/input/directory/$foo/input'
  output: '/my/output/directory/$foo/output'

You can replace $foo inside test-yaml.yml with shell variable $foo by envsubst:

export foo=123
envsubst < test-yaml.yml 

output:

general:
  123: argument_from_bash_script
  rawdatadir: '/some/data/directory/123'
  input: '/my/input/directory/123/input'
  output: '/my/output/directory/123/output'
  • Related