Path: utzoo!attcan!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: alt.sources Subject: Re: (unnamed) - display the effective user name, BSD-ish Message-ID: <2849@kappl.cs.vu.nl> Date: 8 Jul 89 12:40:23 GMT References: <14448@bfmny0.UUCP> <3810@orca.WV.TEK.COM> Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 51 jeff@quark.WV.TEK.COM (Jeff Beadles) writes: \In article <14448@bfmny0.UUCP> tneff@bfmny0.UUCP (Tom Neff) writes: \>If you need a FAST way to generate the name of your current effective \>user (including the effects of su(1) etc), this works. It's not fancy. \>Obviously it has to read /etc/passwd. Feel free to hack and pass \>around. \ \... \# Sed is your friend. Certainly; but are YOU a friend of sed? 1/2 :-) \ID=`id | sed 's/^[^0-9][^0-9]*\([0-9][0-9]*\).*$/\1/'` \#echo "ID: $ID" \ \sed -n "s/^\([a-z][a-z]*\):.*:$ID:.*:.*:.*:.*$/\1/p" < /etc/passwd | line \ \# If you don't have line, use "sed -n 1p" Come on Jeff! Doesn't your version of `id' print something like the following? uid=1234(foo) gid=5(bar) or uid=1234(foo) gid=5(bar) euid=0(root) egid=0(wheel) If so, the following command will print the `effective name' in all cases: id | sed 's/.*uid=[^(]*(\([^)]*\).*/\1/' Else (assuming only numerical uids): ID=`id | sed 's/.*uid=\([0-9]*\).*/\1/'` SED=" /^[^:]*:[^:]*:$ID:.*/{ s/:.*//p q # after the match we can QUIT immediately! } " sed -n "$SED" /etc/passwd Or: ID=... SED=... ypmatch $ID passwd.byuid | sed -n "$SED" -- "... a lap-top Cray-2 with builtin |Maarten Litmaath @ VU Amsterdam: cold fusion power supply" (Colin Dente) |maart@cs.vu.nl, mcvax!botter!maart