[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [K12OSN] Checking for Group Membership



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 <username> <group-to-be-checked>
# Dan Young - Parkrose School District
#
if [ -z "$1" ]; then
	echo usage: $0 "<username> <group-to-be-checked>"
	exit
fi

if [ -z "$2" ]; then
	echo usage: $0 "<username> <group-to-be-checked>"
	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





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]