LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2006, 04:08 AM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
how to get the name of the user who is logged in


Hi,

I am trying to write a script in which I would like to store the name of the user who is logged in and executing the scritp. For e.g. I log on into my system with the username 'tim'.

And then after logging I run a script called ./install and in that script I would like to store the my username(tim) in a variable called $username.

I tried who am i command but it gives other information also and not just the username.

I know it is very simple but I don;t know how to do it.

Thanks.
 
Old 04-28-2006, 04:33 AM   #2
puntjuh
Member
 
Registered: Apr 2006
Location: holland
Distribution: Gentoo / debian / suse / mint
Posts: 558

Rep: Reputation: 42
perhaps:

who -u

?
 
Old 04-28-2006, 05:09 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
if you know their username and want to know what their real name is, try
Code:
finger <user_name>
that should give you any info on them, if they added it when the user was created.

EDIT: although for your purposes, that's probably too much information. when I run whoami, it just gives me my username (currently on RHEL)...

Last edited by pwc101; 04-28-2006 at 05:14 AM.
 
Old 04-28-2006, 05:25 AM   #4
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by puntjuh
perhaps:

who -u

?

No that does not work. It just gives me the name of all the users who are logged in. I just need the name of my own username. I know you might find it strange but I would like to store my own username in a variable in the shell script assuming that I don;t know my own username which I have used to log in.

Hope this clears something.

Thanks
 
Old 04-28-2006, 05:27 AM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by pwc101
if you know their username and want to know what their real name is, try
Code:
finger <user_name>
that should give you any info on them, if they added it when the user was created.

EDIT: although for your purposes, that's probably too much information. when I run whoami, it just gives me my username (currently on RHEL)...
When I give the the command
#who am i

it gives me the following output:
tim pts/22 Apr 28 01:39 (116-54.hsd1.or.comcast.net)

Now this is fine. But I just need the name 'tim' in my shell script variable. How can I do that? Do I have to use the cut or something like that to just get the first word (i.e. tim) from the output.

Thanks
 
Old 04-28-2006, 05:37 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
try
Code:
whoami
as one word.

if you wanted to, you could pipe the output of your original command through awk to get just the first column. something along the lines of
Code:
who am i|awk '{print $1}' > output_file
has the same effect as whoami (all one word)

Last edited by pwc101; 04-28-2006 at 05:41 AM.
 
Old 04-28-2006, 05:43 AM   #7
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Great!!! That worked for me!!! Awesome pwc101.....

 
Old 04-28-2006, 08:40 AM   #8
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Your username is already in a variable, several in fact, namely $USER and $LOGNAME.
 
Old 04-28-2006, 01:31 PM   #9
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by ioerror
Your username is already in a variable, several in fact, namely $USER and $LOGNAME.
Duh. How stupid I am....... I completely forgot about it.

Thanks ioerror...you are the man.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-user and 3D sharing, 3D only works for the first logged in user foxy123 SUSE / openSUSE 0 02-20-2005 04:31 AM
When a user last logged in turbo_acura Linux - General 1 09-21-2004 12:02 PM
i,m logged in as user now what?/debian smuggly Linux - Newbie 1 08-15-2004 03:55 PM
User listed as logged on, even though he's not fishsponge Linux - Security 4 07-19-2004 09:51 AM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration