[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.

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
    return 1

This is how it would be used:

if ismember $user $group; then
    echo "$user is a member of $group"
    echo "$user is not a member of $group"

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

Konstantin ("Icon") Riabitsev
Duke University Physics Sysadmin

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]