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:

#!/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! :slight_smile:
Maybe the "read" is completely wrong to use as well?

Thanks in advance!

I think you were wrogn with syntax. Try the following code instead:

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


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

use the following command inside the script and check
getent passwd <username>

Thanks for the replies, but i still cant get it to work.
When i use this 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:

 #!/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 :confused:
Any suggestions?

Sorry. I made a mistake: then was missing

#!/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


Thank's alot scottn, works like a charm! :b: