[comp.unix.shell] Retrieving unique user numbers

root@oakhill.sps.mot.com (Operator) (04/25/91)

Hello all --

I was asked by a user how to retrieve/identify the unique
user number from within the shell environment (ksh). What
he is looking for would, preferably, function in much the
same fashion as the command [echo `/usr/bin/logname`], where
the result of this op could then be piped into a subsequent
operation. 

If I properly understand his request, what he is trying to 
avoid is invoking an awk script to execute a search of the
/etc/passwd file; however, if an embedded shell command is
not available to fulfill this purpose, then awk it is.

Many thanks,

Vance


-- 
 "I like this company - they hired me, for one; besides that, 
    they haven't asked me to speak for them - so I won't."
/**************************************************************
*       Vance R. Fields III   |   Motorola SPS                *

hansm@cs.kun.nl (Hans Mulder) (04/26/91)

In <1991Apr25.153217.9572@oakhill.sps.mot.com> root@oakhill.sps.mot.com (Operator) writes:

>Hello all --

>I was asked by a user how to retrieve/identify the unique
>user number from within the shell environment (ksh). What
>he is looking for would, preferably, function in much the
>same fashion as the command [echo `/usr/bin/logname`], where
>the result of this op could then be piped into a subsequent
>operation. 

On this system (running SunOS 4.1.1)

id | sed 's/[^=]*=//;s/(.*//'

tells you your uid.  You didn't say what OS you are running.
The man page for id(1v) suggest that it is a SysVism, so it may not be
available on your system.

If you're worried about the efficiency of invoking sed, you'll have to
write a C program that prints the output of the getuid() system call.


BTW, is there any difference between

echo `/usr/bin/logname`

and

logname

??  In general, the echo `...` constructs reduces whitespace, but your
users don't have whitespace in their login names, have they?

>Many thanks,

You're welcome

>Vance

Hans Mulder	hansm@cs.kun.nl

goldfish@concour.cs.concordia.ca (Paul Goldsmith) (04/26/91)

I have a small C program I wrote to find free USER numbers with range
checks and format options.  I posted it to the Comp.Sys.Apollo group a
while ago.  Please send me E-Mail if you want a copy.

--
--	  Paul Goldsmith
<goldfish@concour.cs.concordia.ca>				 (514) 848-3031
	(Shirley Maclaine told me there would be LIFETIMES like this)

mike@bria.UUCP (Michael Stefanik) (04/27/91)

In an article, root@oakhill.sps.mot.com (Operator) writes:
|I was asked by a user how to retrieve/identify the unique
|user number from within the shell environment (ksh). What
|he is looking for would, preferably, function in much the
|same fashion as the command [echo `/usr/bin/logname`], where
|the result of this op could then be piped into a subsequent
|operation. 

How about ...

myuid() {
	expr "`id`" : 'uid=\([0-9]*\).*'
	}

echo "My uid is `myuid`"
-- 
Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
If MS-DOS didn't exist, who would UNIX programmers have to make fun of?

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (04/28/91)

In article <3483@wn1.sci.kun.nl> hansm@cs.kun.nl (Hans Mulder) writes:
>In <1991Apr25.153217.9572@oakhill.sps.mot.com> root@oakhill.sps.mot.com (Operator) writes:
>
>>Hello all --
>
>>I was asked by a user how to retrieve/identify the unique
>>user number from within the shell environment (ksh). What
>>he is looking for would, preferably, function in much the
>>same fashion as the command [echo `/usr/bin/logname`], where
>>the result of this op could then be piped into a subsequent
>>operation. 
>
>On this system (running SunOS 4.1.1)
>
>id | sed 's/[^=]*=//;s/(.*//'

Ik ik ik. This looks cooler:

% perl -e 'print $<;'

And it works on more systems than just SunOS 4.1.1.

		-Kartik
--
internet# rm `df | tail +2 | awk '{ printf "%s/quotas\n",$6}'`

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

cliff@demon.co.uk (Cliff Stanford) (04/28/91)

In article <1991Apr25.153217.9572@oakhill.sps.mot.com> root@oakhill.sps.mot.com (Operator) writes:
>I was asked by a user how to retrieve/identify the unique
>user number from within the shell environment (ksh). What
>he is looking for would, preferably, function in much the
>same fashion as the command [echo `/usr/bin/logname`], where
>the result of this op could then be piped into a subsequent
>operation. 

	echo `grep \^\`/usr/bin/logname\` /etc/passwd | cut -d: -f3`

	Isn't shell quoting wonderful?	:-)
		Cliff
-- 
Cliff Stanford				Email:	cliff@demon.co.uk (Work)
Demon Systems Limited				cms@demon.co.uk   (Home)
42 Hendon Lane				Phone:	081-349 0063	  (Office)
London	N3 1TT	England				0860 375870	  (Mobile)