Re: [K12OSN] Checking for Group Membership

Dan Young (dan_young@parkrose.k12.or.us)
Wed, 29 Jan 2003 13:29:13 -0800


Larry Mateo wrote:
> Red Hat Linux release 7.3
> Kernal: 2.4.18-3smp
>  
> I'm developing and modifying various script files.  I have the need to 
> determine if a user is a member of a specific group, and then do some 
> action(s) based upon the outcome. 
>  
> How would I do this with an "if...then" test or "case" statement, or is 
> there some other clever way?

The "groups" command lists the groups a given user is a member of. My 
shell skills are weak at best, but here's what I came up with. 
Conveniently, regular users can check the groups of other users, so it 
isn't necessary to run this as root. Replace the appropriate bits with 
your own commands:



#!/bin/bash
# usage: groups.sh  
# Dan Young - Parkrose School District
#
if [ -z "$1" ]; then
	echo usage: $0 " "
	exit
fi

if [ -z "$2" ]; then
	echo usage: $0 " "
	exit
fi

USER=$1
GROUP=$2

groups $USER | grep $GROUP &> /dev/null
STATUS="$?"

if [ "$STATUS" = "0" ]; then
	echo $USER is a member of $GROUP # do something useful here
else
	echo $USER is not a member of $GROUP # do something else
fi


--Dan Young
--Parkrose School District



_______________________________________________
K12OSN mailing list
K12OSN@redhat.com
https://listman.redhat.com/mailman/listinfo/k12osn
For more info see