[comp.unix.wizards] shell script puzzler

vsh@etnibsd.UUCP (Steve Harris) (03/21/89)

Here is one for the shell programming gurus:  given the following
shell script, how can I get the subshell to echo its true pid??
(I am running SunOS 3.4, if it matters).

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#! /bin/sh
echo $$			# echos pid of parent shell
( echo $$ )		# echos same pid
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I assume that when the shell encounters the ( list ) construction it
simply forks a copy of itself, with input limited to the list, perhaps
with I/O redirected, but with all variables preserved.  In particular,
the variable $$ seems to be preserved.  Is this assumption correct?

Using this construction (i.e., not replacing it with a here-document,
etc.), is there any SIMPLE way to get the pid of the subshell?

PS -- this puzzler is just for your entertainment, I have plenty of
work-arounds.
-- 
Steve Harris -- Eaton Corp. -- Beverly, MA -- uunet!etnibsd!vsh

chris@mimsy.UUCP (Chris Torek) (03/22/89)

In article <995@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:
>Here is one for the shell programming gurus:  given the following
>shell script, how can I get the subshell to echo its true pid??
>(I am running SunOS 3.4, if it matters).

>#! /bin/sh
>echo $$			# echos pid of parent shell
>( echo $$ )		# echos same pid

At least in 4BSD, you cannot.  `sh' sets up `$$' near the top of main()
and never changes it (except when running a script, in lieu of starting
a new /bin/sh to run it).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

levy@ttrdc.UUCP (Daniel R. Levy) (03/26/89)

[Chris Torek]
< >Here is one for the shell programming gurus:  given the following
< >shell script, how can I get the subshell to echo its true pid??
< >(I am running SunOS 3.4, if it matters).
< 
< >#! /bin/sh
< >echo $$			# echos pid of parent shell
< >( echo $$ )		# echos same pid
< 
< At least in 4BSD, you cannot.  `sh' sets up `$$' near the top of main()
< and never changes it (except when running a script, in lieu of starting
< a new /bin/sh to run it).

Yup that's basically what you have to do, start a new shell explicitly.

If you want to see that pid at the END of the subshell, just

(
...
exec echo '$$'
#nothing from here on will be executed
...
)

Since the exec retains the same pid, it works.  Of course anything from
there to the end of the subshell gets pitched... JUST a picky little
detail....
-- 
Daniel R. Levy             UNIX(R) mail:  att!ttbcad!levy
AT&T Bell Laboratories
5555 West Touhy Avenue     Any opinions expressed in the message above are
Skokie, Illinois  60077    mine, and not necessarily AT&T's.

leo@philmds.UUCP (Leo de Wit) (04/08/89)

In article <3286@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes:
    []
|If you want to see that pid at the END of the subshell, just
|
|(
|...
|exec echo '$$'
|#nothing from here on will be executed
|...
|)

Script started on Sat Apr  8 11:30:51 1989
philmds> (exec echo '$$')
$$
philmds> ^D

script done on Sat Apr  8 11:31:20 1989

A pid of '$$'? C'mon 8-).

What you can do:

	 $ (eval echo '$$')  # or perhaps just (echo $$)

but this will evaluate to the parent's pid (since $$ is set only once,
in the parent; the child is just a forked off copy).
Alternatively,

	 $ sh -c "echo \$\$"

does give its pid correctly. Note however that this requires an exec,
and does not preserve the current shell's context (like shell variables
etc.).

	 Leo.

rbj@dsys.icst.nbs.gov (Root Boy Jim) (05/18/89)

? From: Chris Torek <chris@mimsy.uucp>

? In article <995@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes:
? >Here is one for the shell programming gurus:  given the following
? >shell script, how can I get the subshell to echo its true pid??
? >(I am running SunOS 3.4, if it matters).

I'm running SunOS 3.5.

? >#! /bin/sh
? >echo $$			# echos pid of parent shell
? >( echo $$ )		# echos same pid

? At least in 4BSD, you cannot.  `sh' sets up `$$' near the top of main()
? and never changes it (except when running a script, in lieu of starting
? a new /bin/sh to run it).
? -- 
? In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
? Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

Chris hinted that you must run another sh. Here's how:

	#! /bin/sh
	echo shell pid $$
	(sh -c 'echo subshell pid $$')
	(echo 'echo another subshell pid $$' | sh)
	(eval 'echo why is $$ the parent pid?')
	echo shell pid $$

For some reason, the eval form doesn't work.

	Root Boy Jim is what I am
	Are you what you are or what?