Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

in bash, how to dynamically write use k and v from variable k and v?

$
0
0

we have an arbitrary number of files that I know they have this format

export k="v"

and I know that k does not contain and = sign. I want to treat k as a delimiter.

0.txt

export a="x=y ==\n=/n/ //a=\n:=dd"

1.txt

export b="====x\\nn/ //y=\n:=dd"

2.txt

export My_key="my-value"

etc!

I want to iterate over the files and do these:

  • source the file
  • get the key (everything before the first = sign)
  • get the value
  • create a file named the key from the first step and populate the file with the value from the second step above - The source of truth for the value is the value coming from the env as a result of running the source command

bash.sh file

#!/bin/bashINPUT_DIR="input"OUTPUT_DIR="output"for filename in "$INPUT_DIR"/*; do    source "$filename"    # the three lines below is something I tried and it did not work    # key=$(cat "$filename" | cut -d '=' -f 1 | cut -d '' -f 2)    # value=$(eval echo "\$$key")    # printf "%s" "$VALUE" > "$OUTPUT_DIR/$key"donepython3 test.py

test.py

import osa_env, a_file =  os.environ["a"], open("output" +"/" +"a", "r").read()b_env, b_file =  os.environ["b"], open("output" +"/" +"b", "r").read()Mykey_env, Mykey_file =  os.environ["My_key"], open("output" +"/" +"My_key", "r").read()assert a_env == a_fileassert b_env == b_fileassert Mykey_env == Mykey_file

How can I make it work? The context is that some sidecar container is injecting secrets into files named 0, 1, 2, 3, etc in the input directory with the format of export k="v" - I want those to be available both as an env variable and file and I want the file name to be the key

remember that the source of truth for the value is from env after running the source


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>