0

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                                                                      
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The value of `$test` is the matching line in `/etc/passwd`. Why are you comparing it with `0`? – Barmar Feb 03 '21 at 21:36
  • Use `echo "$test"` to see it. – Barmar Feb 03 '21 at 21:36
  • When you use `(( ... ))` but compare strings that aren't variable names, they are considered equal to `0`; in your case, `test` contains `:`, which throws the `(( .. ))` off. – Benjamin W. Feb 03 '21 at 21:38
  • You should use `[[ ... ]]` to compare strings, but for the better approach (`grep -q`), see the duplicate. – Benjamin W. Feb 03 '21 at 21:38
  • That's why you get a syntax error. The value of `test` is not a number or numeric expression. – Barmar Feb 03 '21 at 21:38
  • the [[ ... ]] does not work either ! – Amine Elabbadi Feb 03 '21 at 21:56
  • Seriously, use the solution in this answer to the duplicate: https://stackoverflow.com/a/11287896/3216427 i.e., `if grep -q "$1" /etc/password; then echo User exists; else echo User does not exist; fi` – joanis Feb 26 '21 at 21:11

0 Answers0