Check Whether a User Exists

后端 未结 17 2038
清歌不尽
清歌不尽 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:56

    I suggest to use id command as it tests valid user existence wrt passwd file entry which is not necessary means the same:

    if [ `id -u $USER_TO_CHECK 2>/dev/null || echo -1` -ge 0 ]; then 
    echo FOUND
    fi
    

    Note: 0 is root uid.

提交回复
热议问题