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 add another domain to their account I want to verify if the user is actually a member of the 'customers' group. What would be the best way to do this?
I have searched google, but all it comes up with is ways to check whether a user or a group exists, so google is not being a big help right now.
A slightly more error-proof method to check for group membership using zero char delimited fixed string grep.
or using long opts
Using the zero delimiter to split by lines:
My version not relying on grep.
First parameter (mandatory): group
Second parameter (optional, defaults to current user)
Try doing this :
or
Explanation:
id -nG $USER
shows the group names a user belongs to.grep -qw $GROUP
checks silently if $GROUP as a whole word is present in the input.I have to clear one thing:
groups
will probably return something like this:But there is one cornercase when your user is named
customers
:For example,
\bcustomers\b
pattern is going to find the username, not the group. So you have to make sure that it is not the first word in the output.Also, another good regexp is: