(unnamed) - display the effective user name, BSD-ish

Maarten Litmaath maart at cs.vu.nl
Sat Jul 8 22:40:23 AEST 1989


jeff at quark.WV.TEK.COM (Jeff Beadles) writes:
\In article <14448 at bfmny0.UUCP> tneff at 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 at cs.vu.nl, mcvax!botter!maart



More information about the Alt.sources mailing list