LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-20-2011, 01:44 PM   #1
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Rep: Reputation: 15
Bash command to find out if user exists


Hi!
Is there any linux command to find out if a user exists?
It should something like this: if user exists it returns 1, if he doesn't it returns 0.

regards,
 
Old 01-20-2011, 02:06 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Assuming the system is using /etc/passwd:
Code:
username=androcles
grep -q "^$username:" /etc/passwd
 
Old 01-20-2011, 02:09 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,844

Rep: Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627
How about
Code:
grep -q '^username:' /etc/passwd
Since it won't return anything you could check the exit status.
Alternatively you could use
Code:
grep -c '^username:' /etc/passwd
which will return the number of matches. Since a username is supposed to be unique it will return 1 on success and 0 if the user is not found.
 
Old 01-20-2011, 02:13 PM   #4
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
nice
thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script with find command dexznrl Programming 6 08-10-2009 01:52 PM
bash command - find |cp crackpipe Linux - Software 4 05-05-2009 03:36 AM
Bash - find command And_pas Programming 7 05-10-2007 04:33 PM
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-31-2004 12:37 AM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 09:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration