From: moonhkt on
Hi All

When using su - jim, how to shown who start a subshell ?

e.g. my id is moonhkt, then su - jim, How to shown moonhkt in
subshell ?



man
...
3 To set up the environment as if you had logged in as the
jim user, type: su - jim

This starts a subshell using jim's login environment.
From: Barry Margolin on
In article
<03be629a-8dbf-4473-a465-bf8c2a899a10(a)n37g2000prc.googlegroups.com>,
moonhkt <moonhkt(a)gmail.com> wrote:

> Hi All
>
> When using su - jim, how to shown who start a subshell ?
>
> e.g. my id is moonhkt, then su - jim, How to shown moonhkt in
> subshell ?

ppid=`ps -o ppid -p $$ | tail -1`
gppid=`ps -o ppid -p $ppid | tail -1`
gpuser=`ps -o user $gppid | tail -1`

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Geoff Clare on
Barry Margolin wrote:

> ppid=`ps -o ppid -p $$ | tail -1`
> gppid=`ps -o ppid -p $ppid | tail -1`
> gpuser=`ps -o user $gppid | tail -1`

No need for tail:

ppid=`ps -o ppid= -p $$`
gppid=`ps -o ppid= -p $ppid`
gpuser=`ps -o user= -p $gppid`

--
Geoff Clare <netnews(a)gclare.org.uk>


From: Barry Margolin on
In article <slrnhuiqjn.4df.houghi(a)penne.houghi>,
houghi <houghi(a)houghi.org.invalid> wrote:

> Geoff Clare wrote:
> > Barry Margolin wrote:
> >
> >> ppid=`ps -o ppid -p $$ | tail -1`
> >> gppid=`ps -o ppid -p $ppid | tail -1`
> >> gpuser=`ps -o user $gppid | tail -1`
> >
> > No need for tail:
> >
> > ppid=`ps -o ppid= -p $$`
> > gppid=`ps -o ppid= -p $ppid`
> > gpuser=`ps -o user= -p $gppid`
>
> After a `su test` where I become the user "test" it indicates root:
> houghi(a)penne : sh /tmp/tt
> ++ ps -o ppid= -p 7296
> + ppid=' 4436'
> ++ ps -o ppid= -p 4436
> + gppid=' 4380'
> ++ ps -o user= -p 4380
> + gpuser=houghi
> [~]
> houghi(a)penne : su test
> Password:
> test(a)penne:/home/houghi> sh /tmp/tt
> ++ ps -o ppid= -p 7339
> + ppid=' 7311'
> ++ ps -o ppid= -p 7311
> + gppid=' 7307'
> ++ ps -o user= -p 7307
> + gpuser=root
>
> Anything I am doing wrong?

The commands have to be run in the shell that su starts up. The first
command gets the PID of the parent process (su), the second command gets
the PID of the grandparent (the shell in which you ran su), and the
third command gets that process's username. By running the commands in
a shell script, you've added another generation of processes, so you
have to go back to the great grandparent.

Try: source /tmp/tt

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Jonathan de Boyne Pollard on
>
>> ppid=`ps -o ppid= -p $$`
>> gppid=`ps -o ppid= -p $ppid`
>> gpuser=`ps -o user= -p $gppid`
>
> After a `su test` where I become the user "test" it indicates root:
> [...]
> Anything I am doing wrong?
>
Yes. You're putting M. Clare's procedure into a script by r�te without
thinking about what the procedure actually does, and thus how and why it
works. Hint: What process is executing the commands when your script is
running, and what process is executing the commands in M. Clare's
original explanation? What are the commands in fact doing?

Once you've worked that out, try this extra hint: Why is "who -m"
(a.k.a. "who am i") the more usual answer to the original question?