-1

Trying to variable replace a templated yaml file.

I'm using eval to take the environment shell variables and replace whats in the file dynamically. I can't figure out how to take the output of this and save to a file.

I just want to take the evaluated output and save to a file.

eval "cat <<EOF
$(<${baseFileName})
EOF"

Exmaple test.yaml

---
value: ${PORT}

Bash environment variable:

PORT=8888

output temp.test.yaml

---
value: 8888

Right now the code will just print the evaluated text to the console.

I've tried.

eval "cat <<EOF
$(<${baseFileName})
EOF" > $newBaseFileName

but no joy. Didn't even create the file.

The reason I'm not using sed is because the file could have unlimited variable decelerations, and I want to replace any value matching a defined bash variable or environment variable. This is part of a template engine. For the life of me I can't remember how I did it before with pure bash.

Grant Zukel
  • 1,153
  • 2
  • 24
  • 48
  • Please give more details. To start, what does the evaluated output look like? See [mcve] – wjandrea Jul 07 '19 at 19:56
  • @wjandrea added since you didn't understand what I was say. Please see context additions. – Grant Zukel Jul 07 '19 at 20:01
  • Your example code actually worked for me. Please make a [mcve] – wjandrea Jul 07 '19 at 20:09
  • 1
    `PORT="$PORT" envsubst < test.yaml` or `export "$PORT"` before calling `envsubst`. If it's not clear how to get that to do exactly what you want then ask a new question with that as your starting point. – Ed Morton Jul 07 '19 at 21:24
  • Did you get `bash: syntax error near unexpected token 'newline'` ? Then check your variable `newBaseFileName`, perhaps first write to `/tmp/envtest.out`. – Walter A Jul 07 '19 at 21:59

1 Answers1

-1

It didn't work for me but what I did is this

renderTemplate() {

eval "cat <<EOF
$(<${1})
EOF"

}

baseFileName=$(basename $fileName)
templateOutput=`renderTemplate ${baseFileName}`
echo "${templateOutput}"

I'm using this as a temp file anyways so what I'll do is save to variable and then pump that variable in to the command to apply the template as a file. That way it's only ever stored in memory. This is a middleware cli to another cli to add variable replacement to in-memory web hosted files before applying them.

Thanks for your help.

Grant Zukel
  • 1,153
  • 2
  • 24
  • 48