Check if user exists shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if user exists shell
# 1  
Old 12-03-2009
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 system.
It then checks if the user exists or not.
If the user exist, it returns something like "The user exist" and if the user doesn't exitst it says "The user doesn't exist".
I have tried many different ways to solve this. And where i'm at now looks something like this:

Code:
#!/bin/sh
echo "Search user:"
dir="/home"
read typed
if
$typed -e $dir
echo "user exist!"
elif
echo "user deosn't exist"
fi

The dir="/home" i understand can be changed to the etc/passwd directory, right? The code complains at the -e command and doesn't like the if statements either. You skilled coders might think i'm stupit to have structured it this way, fell free to right me on this! Smilie
Maybe the "read" is completely wrong to use as well?

Thanks in advance!
# 2  
Old 12-03-2009
I think you were wrogn with syntax. Try the following code instead:
Code:
 #!/bin/sh
echo "Search user:"
dir="/home"
read typed
if
$typed -e $dir
then
echo "user exist!"
else
echo "user deosn't exist"
fi

# 3  
Old 12-03-2009
Code:

#!/bin/sh
echo "Search user:"
read typed


if id $typed > /dev/null 2>&1
   echo "user exist!"
else
  echo "user deosn't exist"
fi

# 4  
Old 12-03-2009
use the following command inside the script and check
getent passwd <username>
# 5  
Old 12-03-2009
Thanks for the replies, but i still cant get it to work.
When i use this code:
Code:
#!/bin/sh 
echo "Search user:" 
read typed 
if id $typed > /dev/null 2>&1 
   echo "user exist!" 
else 
  echo "user doesn't exist" 
fi

I get this error:
sh-3.2# sh ex
Search user:
Simon
': not a valid identifier
ex: line 8: syntax error near unexpected token `fi'
ex: line 8: `fi'
sh-3.2#


And when i try this code:
Code:
 #!/bin/sh 
echo "Search user:" 
dir="/home" 
read typed 
if 
$typed -e $dir 
then 
echo "user exist!" 
else 
echo "user deosn't exist" 
fi

I get this error:
Simon
': not a valid identifier
: command not found
ex: line 6: -e: command not found
: command not found
user exist!
: command not found
user deosn't exist
ex: line 11: syntax error near unexpected token `fi'
ex: line 11: `fi'
sh-3.2#

I've tried alot of combinations and also to change the directory, but nothing seems to work :/
Any suggestions?
# 6  
Old 12-03-2009
Sorry. I made a mistake: then was missing

Code:
#!/bin/sh
echo "Search user:"
read typed
if id $typed > /dev/null 2>&1
then
   echo "user exist!"
else
  echo "user doesn't exist"
fi

# 7  
Old 12-03-2009
Thank's alot scottn, works like a charm! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

2. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

3. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

4. 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

5. Shell Programming and Scripting

C shell scripting, check if link exists on remote servers

Hi, I'm new to C Shell programming. I'm trying to check if a sym link exists on remote server if not send email. I'm not having much luck. Can anyone help? Here is what I have written but it doesn't work. It tells me that my variable was not defined. Here is part of the script, the second... (0 Replies)
Discussion started by: CDi
0 Replies

6. Homework & Coursework Questions

shell to check user is logged on

I was given this to do, Write a Shell script to automatically check that a specified user is logged in to the computer. The program should allow the person running the script to specify the name of the user to be checked, the frequency in seconds at which the script should check. If a... (0 Replies)
Discussion started by: operator
0 Replies

7. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

8. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: _Spare_Ribs_
4 Replies

10. 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
Login or Register to Ask a Question