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.

1
  • getGlobalParameters.sh - is not a line inside Caller.sh, right? It is supposed to be a headline indicating the code below belongs to getGlobalParameters.sh, right? Two separate files, two separate code snippets. I can fix the formatting of the question, but please confirm I understand what you wanted to post. Sep 13, 2022 at 9:21

2 Answers 2

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
1
  • 1
    Thanks, changing the $GETGLOBALPARAMETERS as mentioned by Romeo, has resolved the issue. Sep 14, 2022 at 10:36
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:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .