0

My project has multiple environment variables. Different parts of the pipeline require different subsets of these.

I create the variables and make a table like this:

timestamp=01-02-03T04:05
version=1.2.3

table='
VAR_NAME   DJANGO_ENV   K8S_ENV
timestamp  ✅           ✅
version    ✅           ⛔️
'

Now I would like to populate DJANGO_ENV and K8S_ENV:

DJANGO_ENV="timestamp=01-02-03T04:05 version=1.2.3"
K8S_ENV="timestamp=01-02-03T04:05"

... so I can run $K8S_ENV create_k8s_infra

How to do it?

(Note: I'm answering my own question in case the code is useful to someone)

P i
  • 29,020
  • 36
  • 159
  • 267

2 Answers2

0
timestamp=01-02-03T04:05
version=1.2.3

table='
VAR_NAME   DJANGO_ENV   K8S_ENV
timestamp  ✅           ✅
version    ✅           ⛔️
'

{
    read -r # first line is blank
    read -r first_nonblank_line
    read -r -a env_names <<< "$first_nonblank_line"
    echo "Env-names: ${env_names[@]}"

    while read -r line; do 
        if [[ -n $line ]]; then
            echo
            echo "line=$line"

            read -r -a arr <<< "$line"

            nCols=${#arr[@]}

            var_name=${arr[0]}

            for (( i=1; i<${nCols}; i++ )); do
                echo "    Col#${i} "
                if [[ ${arr[i]} == "✅" ]]; then
                    env_name=${env_names[i]}
                    echo "    Adding $var_name to $env_name"
                    printf -v "$env_name" '%s' "${!env_name} $var_name=\"${!var_name}\""
                fi
            done
        fi
    done
} <<< "$table"

echo "DJANGO_ENV: $DJANGO_ENV"
echo "K8S_ENV: $K8S_ENV"

Output:

> ./table.sh 
Env-names: VAR_NAME DJANGO_ENV K8S_ENV

line=timestamp  ✅           ✅
    Col#1 
    Adding timestamp to DJANGO_ENV
    Col#2 
    Adding timestamp to K8S_ENV

line=version    ✅           ⛔️
    Col#1 
    Adding version to DJANGO_ENV
    Col#2 
DJANGO_ENV:  timestamp=01-02-03T04:05 version=1.2.3
K8S_ENV:  timestamp=01-02-03T04:05
P i
  • 29,020
  • 36
  • 159
  • 267
0

Try eval:

eval $K8S_ENV create_k8s_infra

If it doesn't work, you can do these separately:

eval $K8S_ENV
create_k8_infra

(Reason why may not have worked the first time : here)

Also instead of using printf to populate variables, you could've done it directly:

timestamp=01-02-03T04:05
version=1.2.3

table='
VAR_NAME   DJANGO_ENV   K8S_ENV
timestamp  ✅           ✅
version    ✅           ⛔️
'

{
    read -r # first line is blank
    read -r first_nonblank_line
    read -r -a env_names <<< "$first_nonblank_line"
    echo "Env-names: ${env_names[@]}"

    while read -r line; do 
        if [[ -n $line ]]; then
            echo
            echo "line=$line"

            read -r -a arr <<< "$line"

            nCols=${#arr[@]}

            var_name=${arr[0]}

            for (( i=1; i<${nCols}; i++ )); do
                echo "    Col#${i} "
                if [[ ${arr[i]} == "✅" ]]; then
                    echo "    Adding $var_name to ${env_names[i]}"
                    eval ${env_names[i]}=\"\${${env_names[i]}} $var_name=\"${!var_name}\"\"
                fi
            done
        fi
    done
} <<< "$table"

echo "DJANGO_ENV: $DJANGO_ENV"
echo "K8S_ENV: $K8S_ENV"
Himanshu Tanwar
  • 198
  • 1
  • 11