Programming and Scripting :: Beta testers needed



Quote (Zucca @ Jan. 31 2008,10:06)
Quote (^thehatsrule^ @ Jan. 30 2008,14:23)
I think that's because $() uses a subshell in order to execute the command substitution... so the variable is lost afterwards.

But I don't have any "$(command)" there.
I read that to set the variable local only you must use local -command.

But if it is really lost somewhere, is there a way to pass it to the outer process?

Well, `` does the same thing as $() or maybe you weren't looking where parsememinfo was being called (in the memfree function)?

It's not local variables that you are looking at right now... it's the child subshell(?) that cannot pass a variable (directly) to a parent - hence my original suggestion.

Ok. I think now how I will manage this problem...
The easiest way to get a variable value out of a command substition is to echo it into a variable.

Say I have a function func that produces a result in variable RES, and I call func via command substitution (hence in a subshell).  I can get RES into ANSWER this way:
Code Sample

func(){
# blah
RES=5
echo $RES
}

ANSWER=$(func)


In long messy scripts, this can be a good way to pass variables out of functions without getting mixed up global variables.  Klaus Knopper does this all the time.

Note anything written to stdout from func will go into ANSWER, not to the terminal.

If you need something in func to go to the terminal and not go into ANSWER, re-direct that line's stdout to /dev/tty

New version.

I made it faster by modifying functions.
Those who like to test it will find it from here.

As always, bug reports are welcome. =)


original here.