Global variable value

Hi All,

Im new to shell scripting. I am running EgA.sh and setting one global variable XYZ=0 . Also calling another EgB.sh from EgA.sh, changing the value of XYZ=10
but after executing EgB.sh, value of XYZ is still 0. Im expecting it to be 10.

Anyone for help. Thanks in Advance. :slight_smile:

Variables can be exported in the environment and thus are visible to called scripts and/or programmes, but the environment changes by a called process cannot "trickle up" to the caller.

If you have one value from the called script that you'd like to have in the parent, you can write it to standard out and have the parent assign the output to a local variable.

export result=5         # place into environment for other scripts
result=$(script_b.sh)
echo "result = $result"

and in script_b

result=$(( $result * 2 ))
echo $result

This is not a perfect solution as everything that script B echoes will be assigned to the variable in script A, but it might do what you need.

Thanks Agama, for quick reply. But my script_b.sh doing some other work also and hence return value is not like what Im expecting, as you said and I tried also.

If you want to post the scripts someone might have a suggestion that might get you what you need.

Hi Agama,

Basically I have to install oracle using silent installer .. and after installing oracle, need to create user and Database using the script.
So what I am doing... im calling oracle installer script from main script and when that script finish its work(i.e oracle installation) i need to create users and DB using the main script but that part of script must execute when oracle installation finished by the other script.

Hope this will explain the requirement. :slight_smile:

Well, I'm still confused. I've never installed Oracle so I'm not sure what all is involved there. I'd assume that the installation script detaches from the parent and thus control is returned immediately as the installation continues to work asynchronously and you are having difficulty determining when the install is complete. If not, then it would seem that your control script would block nicely until the install is finished, and then you could continue on as desired.

If the install is running asynchronously, then you might be able to 'watch' it execute using pgrep or similar command blocking in your script until the process has exited. Of course this is problematic as there may not be any way of telling if the install was successful.

Maybe someone with some Oracle experience will chime in with some suggestions.

1 Like