Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi all visitors
i want to write one shell script about user account
how can i compare user account in system and the user that i inputted in shell script?
Thanks
Posted
Comments
Richard MacCutchan 29-Sep-11 12:20pm    
What do you mean by compare? Are you checking the user name as a string, the user's actual id number, or ...?
soeun tony 29-Sep-11 12:36pm    
Thanks for your reply
in my shell script, i will let user to input user name.
example: user input new user name is "jonh"
so i want to compare the new user name "jonh" with old user in system.
Thanks

1 solution

Use the id[^] command, discard any output and check only the exit status. The status is 0 if the username already exists in the user database, 1 if it doesn't exist.
For example, assuming the name to be tested is in the variable $NEWUSER
id $NEWUSER &> /dev/null
if [ $? -eq 0 ]
then
  echo "user $NEWUSER arlready exists"
else
  echo "$NEWUSER is a new name"
fi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900