5
Contributors
5
Replies
39
Views
4 Years
Discussion Span
Last Post by CrazyDieter
0

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
1

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
Votes + Comments
That's very cool, thanks for taking the time to to share the improvement.
0

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.

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.