[prog] user bash script

Dominik Schramm dominik.schramm at slivery.cotse.net
Fri Aug 12 19:36:06 EST 2005


Hi,

John Clarke <johnc+linuxchix at kirriwa.net> writes:

>     if test -n "`grep -i ^$uname: /etc/passwd`"
>     then
>         echo "User $uname already exists"
>         exit 1
>     fi

and you could further shorten even this:

if grep -q -i "^$uname:" /etc/passwd
then
        ...
fi

or:

if grep -qi "^$uname:" /etc/passwd
then
        ...
fi

grep returns 0 in case of matches, 1 otherwise.
"-q" means suppress output (i.e.: of matches). 

That leaves the return code, which is enough to decide whether the
user exists or not. 

dominik


More information about the Programming mailing list