Assign variable value from file to another with the same name

Hi All,

I need to read two config files in a shell script. In that I need to assign a value from one config file to another. I 'm using bash.

config_env.txt

prefix=tab_

config_properties.txt

table_name=${prefix}account

So, when I read these two files in a shell script, I need to substitute the value of prefix in the variable table_name and then output it to a different file.

I could get the value to be printed in the shell script but I need to pass these variables to a different program which I 'm not able to get it work.

For now I managed to work like this:

. ./config_env.txt
. ./config_properties.txt
echo "table name is : $table_namef"

Output is

table name is : tab_account

How do I get this to write to a different output file?

Thanks
Shash

Inside the script?:

. ./config_env.txt
. ./config_properties.txt
echo "table name is : $table_name" > different_output_file

Not sure if this is safe:

. ./config_env.txt
while read line
do
   eval echo $line >>new_properties.txt
done < ./config_properties.txt

Each line is evaluated first, expanding any shell variables into their respective values. The problem comes when malicious commands are included in the file. But given that the file is essentially a configuration shell script, it should be safe (or you are already screwed).

Andrew

1 Like

This works but there are something which I don't want it to be replaced though. Any other better way please?

---------- Post updated at 08:56 AM ---------- Previous update was at 08:55 AM ----------

Sorry, I meant inside a shell script.

Thanks

So you want to expand certain variables and leave the rest alone?

Yes please

So let me be sure I understand what you are saying...

  • You have two scripts that define a bunch of variables.
  • You want another script to get the values of some of the variables defined in each of those two scripts (without saying which ones they are).
  • You want that third script to ignore the values of some of the variables defined in each of those two scripts (without saying which ones they are).

How are we supposed to guess at which variables are to be defined and which variables are to be ignored in your third script?