Add "not" to if statement in shell script

Posted by John Crawford on Ask Ubuntu See other posts from Ask Ubuntu or by John Crawford
Published on 2013-09-13T22:18:05Z Indexed on 2014/06/08 21:41 UTC
Read the original article Hit count: 296

Filed under:
|

I have the following script that should exist if the user does not exist.

#check if user currently exists on system
if id $User > /dev/null 2>&1
then
    #user exists no need to exit program
    echo "blah blah, what a waste of space"
else
    echo "This user does NOT exists.  Please create that user before using this script.\n"
    exit
fi

My problem is that I would ideally like to place a "not" if that first if statement so that I can trim down my if, else statement. Ideally I would like something like this:

if !(id $User > /dev/null 2>&1)
then
    echo "This user does NOT exists.  Please create that user before using this script.\n"
    exit
fi

© Ask Ubuntu or respective owner

Related posts about bash

Related posts about shell-scripting