[comp.unix.xenix] Finding the user name via the shell

mdella@polyslo.CalPoly.EDU (Marcos R. Della) (12/01/88)

I was wondering if anyone out there knew how to determine the users
login name while running a shell script. I can get the home address
$HOME, but not the name. This is under sco xenix 2.2.3 I think...
 
Marcos

-- 
..!csustan ->!polyslo!mdella    | mdella@polyslo | Whatever I said doesn't
..!sdsu ---/   Marcos R. Della  | (805) 543-0135 | mean diddly as I forgot
..!csun --/    225 N. Chorro St                 / it even before finishing
..!dmsd -/     San Luis Obispo, CA 93401       / typing it all out!!! :-)

chip@vector.UUCP (Chip Rosenthal) (12/02/88)

In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
>I was wondering if anyone out there knew how to determine the users
>login name while running a shell script.

I won't say the answer ... but I will tell you how to find out.

Use the "permuted index" at the end of the "User's Reference".  Whenever
you have a question like this, try the index first -- it's really helpful.
If you strip the original question down to the three significant words,
you end up with:

		user's		login		name

Try looking them up in the permuted index, and see what you can find.
This method will generally get you to what you need.

Hint:  in this case, all three of those words will get you to the right place.
-- 
Chip Rosenthal     chip@vector.UUCP    |      Choke me in the shallow water
Dallas Semiconductor   214-450-5337    |         before I get too deep.

daveh@marob.MASA.COM (Dave Hammond) (12/02/88)

In article <6323@polyslo.CalPoly.EDU> Marcos R. Della writes:
>I was wondering if anyone out there knew how to determine the users
>login name while running a shell script. I can get the home address
>$HOME, but not the name. This is under sco xenix 2.2.3 I think...

Try `logname'  (RTFM: logname(C)).

--
Dave Hammond
...!uunet!masa.com!{marob,dsix2}!daveh

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (12/03/88)

In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
| I was wondering if anyone out there knew how to determine the users
| login name while running a shell script. I can get the home address
| $HOME, but not the name. This is under sco xenix 2.2.3 I think...

There is a logname command which has been around since 2.1.2 at least. I
believe the name is read from wtmp or some such.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

tkevans@fallst.UUCP (Tim Evans) (12/03/88)

In article <6323@polyslo.CalPoly.EDU>, mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
> I was wondering if anyone out there knew how to determine the users
> login name while running a shell script.

Use the Xenix 'logname' command.  To use it in shell scripting, see the
following example:

	#get long listing of $HOME directory and mail to me
	ls -l $HOME | mail `logname`

Other System V *NIX's support the environmental variable $LOGNAME, but Xenix
don't!

-- 
UUCP:  ...!{rutgers|ames|uunet}!mimsy!aplcen!wb3ffv!fallst!tkevans
INTERNET:  tkevans%fallst@wb3ffv.ampr.org
OTHER: ...!attmail!fallst!tkevans
Tim Evans  2201 Brookhaven Court, Fallston, MD  21047   (301) 965-3286

mikej@tfli.UUCP (Michael R. Johnston) (12/03/88)

In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
>I was wondering if anyone out there knew how to determine the users
>login name while running a shell script. I can get the home address
>$HOME, but not the name. This is under sco xenix 2.2.3 I think...

The correct answer is to use the variable $LOGNAME. That will return the users
login name. The users REAL NAME is a different story. That could be done as
follows:

REALNAME=`grep "^$LOGNAME:" /etc/password|cut -d: -f5`

Thats all folks!


-- 
---
                Michael R. Johnston - @NET: mikej@cpmain.uucp
...{cmcl2!phri!,uunet!}dasys1!cpmain!mikej || ...!philabs!mergvax!cpmain!mikej

mikej@cpmain.UUCP (Michael R. Johnston) (12/04/88)

In article <249@tfli.UUCP>, mikej@tfli.UUCP (Thats me...) writes:
> 
> REALNAME=`grep "^$LOGNAME:" /etc/password|cut -d: -f5`

CORRECTION:                   /etc/passwd




-- 
                Michael R. Johnston - @NET: mikej@cpmain.uucp
...{cmcl2!phri!,uunet!}dasys1!cpmain!mikej || ...philabs!mergvax!cpmain!mikej

daveh@marob.MASA.COM (Dave Hammond) (12/05/88)

In article <481@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
>following example:
>
>	#get long listing of $HOME directory and mail to me
>	ls -l $HOME | mail `logname`
>
>Other System V *NIX's support the environmental variable $LOGNAME, but Xenix
>don't!

If you place the line:

LOGNAME=`logname`; export LOGNAME

in /etc/profile, then Xenix supports the LOGNAME variable.  This works
for the Bourne and Korn shells.  I'm not a csh'er but I suspect there's
a similar systemwide login commands file.

--
Dave Hammond
...!uunet!masa.com!{marob,dsix2}!daveh

felix@netmbx.UUCP (Felix Gaehtgens) (12/06/88)

In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
>I was wondering if anyone out there knew how to determine the users
>login name while running a shell script. I can get the home address
>$HOME, but not the name. This is under sco xenix 2.2.3 I think...

how about:

set name=`who am i | cut -f1 -d" "`

so long,
			felix

tkevans@fallst.UUCP (Tim Evans) (12/07/88)

In article <249@tfli.UUCP>, mikej@tfli.UUCP (Michael R.  Johnston) writes:
> In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
> >... how to determine the users login name
> 
> The correct answer is to use the variable $LOGNAME.

Although the $LOGNAME variable is supported generally on System V *NIX, SCO
Xenix 2.2 does _not_ support it.  Rather, as I and others have noted here
previously, the Xenix _command_ 'logname' (note lowercase) returns the 
user's login name.

-- 
UUCP:  ...!{rutgers|ames|uunet}!mimsy!aplcen!wb3ffv!fallst!tkevans
INTERNET:  tkevans%fallst@wb3ffv.ampr.org
OTHER: ...!attmail!fallst!tkevans
Tim Evans  2201 Brookhaven Court, Fallston, MD  21047   (301) 965-3286

phile@lgnp1.LS.COM (Phil Eschallier) (12/08/88)

In article <6323@polyslo.CalPoly.EDU>, mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
> I was wondering if anyone out there knew how to determine the users
> login name while running a shell script. I can get the home address
> $HOME, but not the name. This is under sco xenix 2.2.3 I think...
>  

	try this:

	NAME=`logname`
	echo $NAME

tkevans@fallst.UUCP (Tim Evans) (12/09/88)

In article <1794@netmbx.UUCP>, felix@netmbx.UUCP (Felix Gaehtgens) writes:
> In article <6323@polyslo.CalPoly.EDU> mdella@polyslo.CalPoly.EDU (Marcos R. Della) writes:
> >I was wondering if anyone out there knew how to determine the users
> >login name
> 
> how about:
> 
> set name=`who am i | cut -f1 -d" "`

This may not work on SCO Xenix either, unless you have the "text" package.
The 'cut' (and companion 'paste') utilities are not part of the SCO
"runtime" package.  (Public domain versions are available around the net,
and they run on my SCO Xenix 386, Version 2.2.3).  You can use 'awk' to
massage the output of 'who am i'.

-- 
UUCP:  ...!{rutgers|ames|uunet}!mimsy!aplcen!wb3ffv!fallst!tkevans
INTERNET:  tkevans%fallst@wb3ffv.ampr.org
OTHER: ...!attmail!fallst!tkevans
Tim Evans  2201 Brookhaven Court, Fallston, MD  21047   (301) 965-3286

jim@fsc2086.FSC.COM (Jim O'Connor) (12/10/88)

In article <484@fallst.UUCP>, tkevans@fallst.UUCP (Tim Evans) writes:
> In article <249@tfli.UUCP>, mikej@tfli.UUCP (Michael R.  Johnston) writes:
> > 
> > The correct answer is to use the variable $LOGNAME.
> 
> Although the $LOGNAME variable is supported generally on System V *NIX, SCO
> Xenix 2.2 does _not_ support it.  Rather, as I and others have noted here
> previously, the Xenix _command_ 'logname' (note lowercase) returns the 
> user's login name.

Actually, it's just that Xenix 2.2 doesn't set it automatically at login
time.

If you are used to having the $LOGNAME variable present (such as I was when
switching from Altos Xenix to SCO Xenix) and your "login" program won't set
it for you, just add:

LOGNAME=`logname`     # or any program that will return the user's log name
export LOGNAME

to your /etc/profile file or your user's individual profiles and then $LOGNAME
will be available.  This is also handy for other "constants" that usually
require running some program to get the value of.  For example, 

LOCALSYS=`uuname -l`
PORT=`tty`
...
export LOCALSYS PORT . . .

Then, if you write many shell scripts that require these values, you can look 
them up in the environment and save a little fork() and exec() time (same
argument as for shell built-in commands).  But, if you don't use these
values much, you'd probably be better off just running hte program in the
scripts themselves, and save the extra time it would take to process
everyone's login sequence.  It's the old "6 of one, half-dozen of the other"
type thing, and you just have to decide for yourself which one is better.

If you are writing scripts to port to other machines, however, you should
probably use the program in the script, since you wouldn't want to assume
other systems will use the pre-set variables.

--jim
------------- 
James B. O'Connor				jim@FSC.COM
Filtration Sciences Corp.			+1 615 821 4022 x651
105 W. 45th St. - Chattanooga, TN 37409