Hi
I want to check wether user exist in server or not uisng shell script?
can any one help me regarding this

Recommended Answers

All 5 Replies

This script will tell you if a user account exists on the system, just pass the user name you want to check as an argument to the script like user_exists.sh foo

#!/bin/bash

cat /etc/passwd | grep ^$1 >/dev/null

if [ $?  == 0 ];
then
    echo "User $1 exists."
    exit 0
else
    echo "User $1 does not exist."
    exit 1
fi

hi,

uuoc (useless use of cat)
uuot (useless use of test)

if grep -q ^"$1" /etc/passwd
then
    echo "user $1 exists"
else
    echo "user $1 does not exist"
    exit 1
fi
commented: That's very cool, thanks for taking the time to to share the improvement. +1

Hi Friend,
Here i have 1 more arised. my linux box is not a stand alone. This is connected in intrenet my used id is also a domain account.
How to check the domain account.

What type of domain account?

I'd rather use the id(1) command :

if id -u $1
then
    echo "user $1 exists"
else
    echo "user $1 does not exist"
    exit 1
fi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.