Operating System - HP-UX
1753449 Members
6002 Online
108794 Solutions
New Discussion юеВ

problem with global variables

 
SOLVED
Go to solution
itai weisman
Super Advisor

problem with global variables

hello everyone,
I'm trying to automate the procedure of adding nodes to my enviorment by writing a script.
for some reason, i'm not able to use global variables, in some parts of the script.
I have no idea why, the global variable {ExitCode} and {NODE_TYPE}, won't be updated in the outside the function.
since NODE_TYPE contains the value for 'mach_type' parameter of opcnode command,
AddToDB function fails, and since ExitCode variable updated on the function is not updated globally, test for {ExitCode} (line 289), that excute from main, succeed (while its suppose to fail)
attached tar file that contains the script and parameter file (if you try to run the script, modify OVO_SERVER parameter - set on line 30
thanks ppl
5 REPLIES 5
Kent Ostby
Honored Contributor

Re: problem with global variables

I know there are problems passing variables between scripts depending on how the shell is forked.

Is the problem that the variable don't make it from the parent to the child or that they aren't returned from the child to the parent ?

If it's that they are not going from the parent to the child, then you may need to manually "source" the variable file (i.e. if it were a global networking variable then add:
. /etc/rc.config.d/netconf to the script).

Hope that helps.

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
itai weisman
Super Advisor

Re: problem with global variables

hi and thanks,
there are no parsing between scripts, only between diffrent function on the same script.
the thing is, there are varibles that defined first out of the function (globbaly) (1) , than one function use this variable (2), then main proggram use it again (3), and then another function use this variable (4).
sometimes, i see varible that was defined in (2) and won't be recognize at (3) or in (4).
Rory R Hammond
Trusted Contributor

Re: problem with global variables

itai,

I was able to duplicate your problem with the following script.
#
#! /bin/sh
func1()
{
Code=1
return 0
}

func2()
{
Code=2
return 0
}

func3()
{
Code=3
return 0
}

func4()
{
Code=4
func3
return 0
}

for arg in func1 func2 func3 func4
do
$arg | tee -a xxx
echo "${Code}"
done

I was able to make it work with the following changes:

(for arg in func1 func2 func3 func4
do
$arg
echo "${Code}"
done) | tee -a xxx

Hope this helps you in figuring out how to modify your script.

Rory
There are a 100 ways to do things and 97 of them are right
itai weisman
Super Advisor

Re: problem with global variables

hi Rory and thanks,
i don't understand.. where did you put the braces?
(i see a closer after done )
where would you do the return code ( $? ) test?
what about other global parameter that wont be updated? or sometimes being update and sometimes not? maybe i don't use global variable correctly?
Rory R Hammond
Trusted Contributor
Solution

Re: problem with global variables


itai,


( for ... done )|tee -a filename

I think the tee is causing a forked process (child) and the child is not able to update the parents variables.

A another solution would be to update the exetcodes to a tmp exitcode file. then read the results from the file.
You would have to remove the file after shell completion or when trapped.

Hope that helps

rory
There are a 100 ways to do things and 97 of them are right