Check if a user is in a group

后端 未结 13 1898
梦如初夏
梦如初夏 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 03:07

    A slightly more error-proof method to check for group membership using zero char delimited fixed string grep.

    if id -nGz "$USER" | grep -qzxF "$GROUP"
    then
        echo User \`$USER\' belongs to group \`$GROUP\'
    else
        echo User \`$USER\' does not belong to group \`$GROUP\'
    fi
    

    or using long opts

    if id --name --groups --zero "$USER" | 
       grep --quiet --null-data --line-regexp --fixed-strings "$GROUP"
    then
        echo User \`$USER\' belongs to group \`$GROUP\'
    else
        echo User \`$USER\' does not belong to group \`$GROUP\'
    fi
    

提交回复
热议问题