Check Whether a User Exists

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

    Actually I cannot reproduce the problem. The script as written in the question works fine, except for the case where $1 is empty.

    However, there is a problem in the script related to redirection of stderr. Although the two forms &> and >& exist, in your case you want to use >&. You already redirected stdout, that's why the form &> does not work. You can easily verify it this way:

    getent /etc/passwd username >/dev/null 2&>1
    ls
    

    You will see a file named 1 in the current directory. You want to use 2>&1 instead, or use this:

    getent /etc/passwd username &>/dev/null
    

    This also redirects stdout and stderr to /dev/null.

    Warning Redirecting stderr to /dev/null might not be such a good idea. When things go wrong, you will have no clue why.

提交回复
热议问题