Check if a user is in a group

后端 未结 13 2034
梦如初夏
梦如初夏 2021-01-31 02:42

I have a server running where I use php to run a bash script to verify certain information of a user. For example, I have a webhosting server set up, and in order to be able to

13条回答
  •  灰色年华
    2021-01-31 02:57

    if id -nG "$USER" | grep -qw "$GROUP"; then
        echo $USER belongs to $GROUP
    else
        echo $USER does not belong to $GROUP
    fi
    

    Explanation:

    1. id -nG $USER shows the group names a user belongs to.
    2. grep -qw $GROUP checks silently if $GROUP as a whole word is present in the input.

提交回复
热议问题