Passing information from a subshell to its parent

Passing information from a subshell to its parent

Post by R. Bernste » Mon, 17 Feb 2003 13:06:44



Is there a simple way to pass information from a subshell back to its
parent?

I've run across this problem working on a de* for bash,
(http://www.veryComputer.com/) but the problem exists for any shell
language that has subshells. De* routines live in the same
environment as the debugged program - de*s want access to the
debugged program's environment, and this not only the simplest, but in
some cases only way to get this. The debugged program may be stopped
inside a subshell, but generally one wants to think of the de*
having global scope. So for example if I set a breakpoint or set the
number of lines to list in a display while in a subshell, that setting
should persist when I leave the subshell.

I have had to extended bash just to *know* that it is in a subshell.
This is done a new dynamic variable BASH_SUBSHELL. I have a somewhat
awkward mechanism, of writing to a file the state changes to be passed
back to the parent shell when the program/de* is in a
subshell. But then there is next the problem of having the parent pick
up this information. A first-cut idea that *almost* works is to have
the de* always look for a file from the next level subshell, read
that in and then erase it.  Two complications with this. Consider
this:

  ( (cd a ) ; (cd b ) )

The "cd a" and "cd b" are both at the same subshell level, 2, but they
are in at *different* subshells and from a stepping or tracing
standpoint one follows the other without an action in subshell level
1. A simple subshell check of 2 would not be enough for a de* to
know that the subshell 2 of before was different from this
one. Perhaps the PID could be used to determine this. The second
problem is that at the end of that we jump from subshell level 2 to
subshell level 0. So not only would you have to check for my subshell
level+1 but *all* levels greater than mine.

Again for bash, I've started implementing in bash another fake trap
(like DEBUG, or ERR) called SUBEXIT which runs when leaving a
subshell. However right now, there are technical problems with it. It
would be great if a new mechanism were not needed.

It occurs to me this problem of passing information from a subshell to
its parent is not really unique to de*s. A general mechanism
would be useful. Perhaps it already exists and others have found ways
to do this.

Thanks.

 
 
 

Passing information from a subshell to its parent

Post by those who know me have no need of my nam » Tue, 18 Feb 2003 23:35:51


in comp.unix.shell i read:

Quote:>Is there a simple way to pass information from a subshell back to its
>parent?

typically you do this by generating output, which the parent captures then
processes.

Quote:>I have had to extended bash just to *know* that it is in a subshell.
>This is done a new dynamic variable BASH_SUBSHELL.

SHLVL wasn't appropriate?

--
bringing you boring signatures for 17 years

 
 
 

Passing information from a subshell to its parent

Post by Tim Cargi » Wed, 19 Feb 2003 05:59:39



Quote:> in comp.unix.shell i read:

> >Is there a simple way to pass information from a subshell back to its
> >parent?

> typically you do this by generating output, which the parent captures then
> processes.

> >I have had to extended bash just to *know* that it is in a subshell.
> >This is done a new dynamic variable BASH_SUBSHELL.

> SHLVL wasn't appropriate?

Hmmmm.  Doesn't look like it:

mush (2.0): (CYGWIN_ME-4.90) 02/17/03 15:43:31: Start
mush (2.0): (MUSH_LIST=ash bash sh zsh)  Args: -c tx

-------------------- Unknown-type File: tx  

echo "SHLVL = $SHLVL"

( echo "SHLVL = $SHLVL" \( echo "SHLVL = $SHLVL" \) )

-------------------- Shell: ash  File(1): tx  

SHLVL = 2
SHLVL = 2 ( echo SHLVL = 2 )

-------------------- Shell: bash  File(1): tx  

SHLVL = 3
SHLVL = 3 ( echo SHLVL = 3 )

-------------------- Shell: sh  File(1): tx  

SHLVL = 3
SHLVL = 3 ( echo SHLVL = 3 )

-------------------- Shell: zsh  File(1): tx  

SHLVL = 3
SHLVL = 3 ( echo SHLVL = 3 )

-------------------- mush (2.0) 02/17/03 15:43:37 End

Anyway, the 'bash' manual says:

   SHLVL  Incremented by one each time an instance of bash is started.

Note:  I was already at bash SHLVL=2 when each of the above
interpreters was started by the 'mush' utility to run the
file.  All but 'ash' incremented the SHLVL value when it started.

Anyway, the test prompted yet some more tweaks to my 'mush' utility,
if nothing else came of this.  :-)

HTH

Tim - PITA

 
 
 

Passing information from a subshell to its parent

Post by those who know me have no need of my nam » Wed, 19 Feb 2003 08:39:17


in comp.unix.shell i read:



>> in comp.unix.shell i read:
>> >I have had to extended bash just to *know* that it is in a subshell.
>> >This is done a new dynamic variable BASH_SUBSHELL.

>> SHLVL wasn't appropriate?

>Hmmmm.  Doesn't look like it:

that's a bummer.  in all these years i've never had a need for it, merely
presuming that a sub-shell must be another level.  perhaps i did at one
time and it wasn't appropriate and i've just forgotten it -- so much data,
such a small head.

Quote:>Anyway, the test prompted yet some more tweaks to my 'mush' utility,
>if nothing else came of this.  :-)

excellent.

--
bringing you boring signatures for 17 years

 
 
 

1. How the subshell communicate with parent shell?

In the korn shell, one can "export" some enviroment variable to let its
subshell inherited. But how can subshell
communicate with its parent shell? In another word, how can subshell let
its parent know the value of variable
which defined inside this subshell?

Thanks,

--
Sharon Xiaoyun Kong
------------------------------------

Tel: 914-892-4489
IBM internal number: 532-4489

2. messed up script?

3. unblocking a parent shell after it launches a subshell

4. Zoltrix 14.4 int Rockwell

5. what is parent shell, child shell and subshell, ?

6. setting up awe64

7. How to pass an argument to subshell of bash?

8. apache - changing forbidden message?

9. pass array value to subshell ?

10. Cygwin:Bash: Preserve '\' in cmdline arg to pass to subshell?

11. Passing commands to subshell

12. `Nested (`Nested Subshells`) Subshells`

13. tcsh - to subshell or not to subshell