0

I have two shell scripts Caller and getGlobalParameters. There is a function defined in the getGlobalParameters script which calls a java class and uses the values returned by the java class to set the environment variables. Once the call to the getGlobalParameters completes, the caller is trying to use the environment variables set by the getGlobalParameters. The issue I am facing is the environment variables set by the getGlobalParameters are not accessible to the Caller. Below is the code snippet of Caller and getGlobalParameters.

Caller.sh

GETGLOBALPARAMETERS=getGlobalParameters.sh
jobName=ABC
JOBENV=TEST
eval '$GETGLOBALPARAMETERS $jobName $JOBENV'
printenv
#In the printenv statement above I am not seeing the environment variables set by the getGlobalParameters

getGlobalParameters.sh - #Sample OUTPUT value a^1$b^2 IFS='$' read -r -a array <<< "$OUTPUT" declare -A configkeys for element in "${array[@]}" do IFS='^' read -r -a temparray <<< "$element" configkeys[${temparray[0]}]=${temparray[1]} done for key in "${!configkeys[@]}"; do export "${key}="${configkeys[$key]}"" done printenv #In the above printenv I am able to see the environment variables.

Can anyone take a look and let me know what I am missing. Have spent a day already figuring out the issue, but couldn't find any way to fix this.

2 Answers2

0

Your eval '$GETGLOBALPARAMETERS $jobName $JOBENV' effectively means:

getGlobalParameters.sh ABC TEST

where getGlobalParameters.sh is an executable. As any executable it runs in its own environment and it cannot change the environment of its parent. It's "accidental" getGlobalParameters.sh is a script. The script is interpreted probably by the same shell as the parent (Caller.sh), but not by the same instance (process) as the parent. The child process initially inherits the environment from the parent, but then the environments are independent.

Without a debugger, only Caller.sh can change its own environment. You have at least two options:

  1. Either develop some procedure inside Caller.sh to get values from the child (example) and to modify its own environment accordingly.

  2. Or run the "child" shell code (currently in getGlobalParameters.sh) in a way so it's interpreted by the shell interpreting Caller.sh. You can:

0

Beside source way you can change this line:

getGlobalParameters.sh - 

to be

. getGlobalParameters.sh

This will execute the command in current shell and you will be able to access variables set inside.

Same for line

eval '$GETGLOBALPARAMETERS $jobName $JOBENV'

to become

. $GETGLOBALPARAMETERS $jobName $JOBENV
Romeo Ninov
  • 7,848