Good (reliable!) check if user exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Good (reliable!) check if user exists
# 1  
Old 12-18-2006
Question Good (reliable!) check if user exists

Hi all,

I've been trying to find a good check I can put it in to a shell script to see if a given user exists.

Some of the things I've thought about is checking whether they have a home directory, but not all users have a home directory. I've thought about grepping the /etc/passwd file for the username but if the username is short and happens to be a repeated string somewhere else in the file then this check could fail or be inaccurate too.

Ideally I would like a simple command so I can just use it in an if/else statement, so if the user exists then do this and if the user doesn't exist then do something else.

In case it helps it would be using the bash shell and would need to be supported by RedHat 9 and SuSE 10 but that shouldn't make too much difference.

Any ideas would be appreciated no matter how bizzare they may seem Smilie

Thanks.
# 2  
Old 12-18-2006
Quote:
Originally Posted by _Spare_Ribs_
Hi all,

I've been trying to find a good check I can put it in to a shell script to see if a given user exists.

Some of the things I've thought about is checking whether they have a home directory, but not all users have a home directory. I've thought about grepping the /etc/passwd file for the username but if the username is short and happens to be a repeated string somewhere else in the file then this check could fail or be inaccurate too.
You're close to a solution. Just improve your search string enough and you can fix the substring problem:
Code:
grep "^${USERNAME}:" /etc/passwd

The ^ means "from the beginning of the line", and the : is the field-delimiting character in /etc/passwd, so you're guaranteed to match either the entire username field or nothing at all.
# 3  
Old 12-19-2006
Computer

there are lots of ways on checking if aa user exist besides the passwd file. you can use the "id" or the "finger" command which are reliable commands.

hope this helps.

many thanks Smilie
# 4  
Old 12-19-2006
Quote:
Originally Posted by inquirer
there are lots of ways on checking if aa user exist besides the passwd file. you can use the "id" or the "finger" command which are reliable commands.
I wouldn't call finger reliable, being considered an obsolete security hole which most systems no longer bother to install by default. id, on the other hand, is a useful looking thing which I didn't know about. Thanks!
# 5  
Old 12-19-2006
Nice one, totally forgot about the id command.

What's more embarassing is 10 lines up in the code I had the $(id -u) command to check the user was root! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if file exists or not

Hi, I want to check if the file exists or not in the directory. i am trying below code but not working. File="/home/va59657/Account_20090213*.dat" echo "$File" if ]; then echo "file found" else echo "file not found" fi However i am getting file not found even if file exits as... (5 Replies)
Discussion started by: Vivekit82
5 Replies

2. Shell Programming and Scripting

How to check if the URL exists?

Hi, I need to check if the URL exists. Below is my OS: SunOS mymac1 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220 I do not have the curl set in the profile nor am i aware about its path. But i have wget. Please help me with params for the same. Can you help me check if... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. UNIX for Dummies Questions & Answers

How to check if a number exists?

Hello, May i please know how do i check if the given input argument is one of the listed numbers then success else failure. I am using bash shell. if then echo "success" else echo "failure" fi Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

4. Homework & Coursework Questions

C TCP/IP Reliable Transmission project not reliable

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: We must do the following for a massive coding project that is due at 12:20PM on Monday, July 22, 2013. We are to... (1 Reply)
Discussion started by: kowit010
1 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. Shell Programming and Scripting

Check if user exists shell

Hello! I'm stuck with a problem that i can't solve. I'm very new to unix, linux and shell scripting i might add. I'm trying to create a script that will execute as follows: First start the script - sh exist Then the prompt asks the user to input a username to check if it exists within the... (6 Replies)
Discussion started by: bib2006
6 Replies

7. Shell Programming and Scripting

How to check if database exists?

Hi folks! First off I'm working with a Sybase DB. I'm using you're basic ISQL command to connect to my Sybase DB... isql -S$DB_SERVER -D$DB_NAME -U$DB_USR -P$DB_PWD <<!EOF > $log_file My question is, is there a way to determine if a database exists using shell script? For example, if... (2 Replies)
Discussion started by: Fatbob
2 Replies

8. Shell Programming and Scripting

How to check if a direcorty exists?

Hi Good people :D How do I check if a directory exists, if it does then carry on rest of the script, otherwise exit. ------------- cd $mainfolder/system1 #unzips files arrived in last 24 hrs into temp directory find * -mmin -1440 -exec unzip {} \; I'd like to check here if temp... (2 Replies)
Discussion started by: SunnyK
2 Replies

9. SCO

Need Script to check whether user exists in the remote machine

Hi All, I am new to shell scripting. Can someone let me know, how to check whether the user exists in the remote system? I am building a new unix box and before I proceed installing the appliation , I want to check whether the required users are created in the system . how to do this ?... (1 Reply)
Discussion started by: Srini75
1 Replies

10. Shell Programming and Scripting

check if directory exists

Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory. However when I try the following I cannot get it to work. while ... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question