Check if a user is in a group

后端 未结 13 1897
梦如初夏
梦如初夏 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:01

    My version not relying on grep.

    First parameter (mandatory): group
    Second parameter (optional, defaults to current user)

    isInGroup(){
       group="$1"
       user="${2:-$(whoami)}"
       ret=false 
       for x in $(groups "$user" |sed "s/.*://g")
       do [[ "$x" == "$group" ]] && { ret=true ; break ; }
       done
       eval "$ret"
    }
    

提交回复
热议问题