Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems doing a grep from a who command 2

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
Im having a day in which everything I touch is turning brown and sticky...

I want to find the owner and machine hosting a remote tty session. If I type "who" I get

Code:
root     tty1         Feb  1 12:42         
david    pts/0        Feb 10 08:47 (venus) 
godfrey  pts/2        Feb  4 11:42 (saturn)
godfrey  pts/3        Feb 11 17:54 (saturn)
david    pts/1        Feb 10 16:36         
godfrey  pts/4        Feb 11 16:10 (saturn)
browning pts/6        Feb 10 15:07         
browning pts/5        Feb 10 13:55 (venus)

If I do "tty | sed s^/dev/^^" in a sample shell I get the result "pts/3" I then want to extract the relevant line in the "who" result to find out the owner of the tty and the machine (in this example it is godfrey on saturn).

So I tried;
Code:
grep `tty|sed s^/dev/^^` `who`

and

who | grep `tty|sed s^/dev/^^`

but both fail miserably. However "who | grep pts/3" works. If I make a separate env for the tty part and then do the grep with that, it also works.

What am I doing wrong?
 
Hi,

grep `tty|sed s^/dev/^^` `who`
cannot work, because `who` is not a file name;

who | grep `tty|sed s^/dev/^^`
is a bit more complicated.
I think the problem is that the output of tty command is 'not a tty' sometimes, e.g. if it is called by a shell script.

Not sure how you could work around this ...
 
This is due to it being called in a pipe, not related to using it in scripts.

Requires to set the tty as a var first, tty=`tty|sed s^/dev/^^`

another, probably not too portable solution: who | grep "`readlink /proc/$$/fd/1 | sed 's/.dev.//'` "

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
You may try something like this:
s=`tty` who | grep `echo $s | sed 's!/dev/!!'`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You guys are awesome!

Looks like I was partly on the right lines making a new var and using that for the grep (which is what Ive put in the script for now).

Having said that hoinz's "who am i" is exactly what I need. I knew about the whoami command but I never knew about this other flavor.

Thanks again guys.
 
PHV, the reason yours wont work is the same i made tty=.. an own cmd.. you cant assign a var to a starting command where you instantly reuse it shellwise there, i guess cause the shell wont get it.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
PHV, the reason yours wont work(/i]
Have you tried it ?
Works for me (ksh, SCO OSR 5.0.x)
 
PHV, the reason yours wont work
Have you tried it ?
Works for me (ksh, SCO OSR 5.0.x)
 
Yes, bash2/3, zsh 4.2-dev and (pd)ksh v5.2.14 99/07/13.2 on linux. Sorry i shouldnt be that fast-<spikey>, it just looks like exceptional behavior, but we all know sco* != linux. neither *bsd or mainstream.
Ill take more care about such posts later on, sorry again.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
stackdump:
Glad to see it helped; thanks

xmb:
Thank you for setting right my knowledge on the reasons of 'not a tty';
have a star for it!

 
Merci :)

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top