mercredi 29 juin 2016

check for results of `getent passwd user` in bash if statement

A user exists called teststu

Here's my script checkuser.sh

#!/bin/bash
USERINFO=$(getent passwd $1)
echo "User info:  $USERINFO"
if [$USERINFO]; then
   echo "$1 already exists"
else
   echo "New user: $1"
fi

if I run: sh checkuser.sh teststu I get:

User info:  teststu:*:10001:10001:teststu:/home/teststu:/bin/bash
checkuser.sh: 4: checkuser.sh: [teststu:*:10001:10001:teststu:/home/teststu:/bin/bash]: not found

New user: teststu

and if I run sh checkuser.sh nonexistantuser:

User info:
checkuser.sh: 4: checkuser.sh: [-n: not found

New user: nonexistantuser

How can I get my if statement to properly differentiate between these two results?

I was trying to use this:

[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.

Aucun commentaire:

Enregistrer un commentaire