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

Re: [K12OSN] Checking for Group Membership



On Wed, 2003-01-29 at 20:25, Steve Wright wrote:
> >A more flexible way of writting that line is:
> >
> >	if (groups $user | grep $group) >>/dev/null ; then

Warning, this can cause problems! E.g. if $group is "nobody", it will
match both "nobody" and "nfsnobody".

The most sure way is by iterating through the output of id without using
matching, as it's always error-prone. Here is an ismember() function
that I would use.

<snip>
function ismember(){
    # note: these are backticks in the next line, not apostrophes
    for TESTGROUP in `/usr/bin/id -Gn $1`; do
        if [ "$TESTGROUP" = "$2" ]; then
            return 0
        fi
    done
    return 1
}
</snip>

This is how it would be used:

<snip>
if ismember $user $group; then
    echo "$user is a member of $group"
else
    echo "$user is not a member of $group"
fi  
</snip>

This is, perhaps, the most bullet-proof solution. It will work on
various locales, too.

Regards,
-- 
Konstantin ("Icon") Riabitsev
Duke University Physics Sysadmin
www.phy.duke.edu/~icon/pubkey.asc

Attachment: signature.asc
Description: This is a digitally signed message part


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