My script tests if a user exist in /etc/passwd
and echos yes or no.
#!/bin/bash
if (( $# == 0 ))
then
echo "ERROR CODE 1 : no argument ! "
exit
fi
if (( $# > 1 ))
then
echo "ERROR CODE 2 : too many arguments !"
exit
fi
test=0
test=$(cat /etc/passwd | grep $1)
if (( test == 0 ))
then
echo "User Do Not Exist !"
else
echo "User Exist"
fi
It works if I test a user like amine
that doesn't exist:
sysadmin@localhost:~$ bash userExist.sh amine
User Do Not Exist !
It doesn't work if I test an existing user like root
:
sysadmin@localhost:~$ bash userExist.sh root
userExist.sh: line 16: ((: root:x:0:0:root:/root:/bin/bash
operator:x:1000:37::/root:/bin/sh: syntax error in expression (error token is ":
x:0:0:root:/root:/bin/bash
operator:x:1000:37::/root:/bin/sh")
User Exist