Check Whether a User Exists

后端 未结 17 1978
清歌不尽
清歌不尽 2020-11-29 17:31

I want to create a script to check whether a user exists. I am using the logic below:

# getent passwd test > /dev/null 2&>1
# echo $?
0
# getent pa         


        
17条回答
  •  情深已故
    2020-11-29 17:53

    Why don't you simply use

    grep -c '^username:' /etc/passwd
    

    It will return 1 (since a user has max. 1 entry) if the user exists and 0 if it doesn't.

提交回复
热议问题