0

Here is the scenario:

The System is preset with the following Environment Variables:

INNER1=1
INNER2=2
INNER3=3

Now I have the file: values.properties

OUTER1=$INNER1
OUTER2=$INNER2
OUTER3=$INNER3

I also have a shell script named: run.sh

#!/bin/sh

PROPERTIES_FILE=values.properties

while IFS== read -r VAR1 VAR2
do
    export $VAR1=$VAR2
done < $PROPERTIES_FILE

Now the problem is, when I run the script, the following is exported:

OUTER1=$INNER1
OUTER2=$INNER2
OUTER3=$INNER3

But the desired result should be:

OUTER1=1
OUTER2=2
OUTER3=3

I want it to fully resolve the variable name when exporting it.

Mark
  • 49
  • 4
  • 2
    Why complicate things? Just `. values.properties` – Joseph Sible-Reinstate Monica Apr 22 '20 at 00:43
  • How do you run that script? Or where do you use the exported variables? – Benjamin W. Apr 22 '20 at 00:58
  • 1
    For an extensive discussion of indirect expansion in bash, see [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006). That said, if you trust your data to be valid code, I agree with Joseph that you should be running it as code *explicitly* (using the `.` operator, also aliased in bash under the name `source`). – Charles Duffy Apr 22 '20 at 01:56
  • @CharlesDuffy : While the OP tagged this as _bash_, the script is most likely not executed as bash script. Unfortunately, the OP did not tell us how he runs the script, but assuming that he runs it just by entering its path, it is run by `sh`. – user1934428 Apr 22 '20 at 07:01
  • 1
    @user1934428, the BashFAQ also covers POSIX sh and ksh. – Charles Duffy Apr 22 '20 at 11:33
  • @JosephSible-ReinstateMonica I didn't want to add to the complexity of the request. But the reason I don't export the values directly is because there should be an IF statement which can choose among several values.properties, e.g: values1.properties, values2.properties, etc. – Mark Apr 22 '20 at 13:52
  • @Mark : It is not clear to me what you mean by _choosing among ..._. Maybe this is a kind of complexity which needs to go into the question, if you want to have it answered. Also, you need to finally clarify whether you are going for _bash_ (as your tag says) or for _sh_ (as your code says), because - if you have read the article linked to by Charles Duffy - you know that this makes a big differece, in particular in your concrete problem. – user1934428 Apr 23 '20 at 07:45

1 Answers1

0

You need to make use of shell parameter expansion. #!/bin/sh

PROPERTIES_FILE=values.properties

while IFS== read -r VAR1 VAR2
do
  export $VAR1="${!VAR2}"
done < $PROPERTIES_FILE

You may also need to change your values.properties file to:

OUTER1=INNER1
OUTER2=INNER2
OUTER3=INNER3

I hope this helps!

Mackaber
  • 43
  • 6
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section *Answer Well-Asked Questions*, and the bullet point therein about questions that "have already been asked and answered many times before". Answering duplicates means that folks are less likely to see the more complete and extensively-vetted set of answers on the question's canonical instances. (Of course, you should feel encouraged to add a new answer on the canonical instance if the ones already there are missing something!) – Charles Duffy Apr 22 '20 at 01:50