Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check if a group exists

366 views
Skip to first unread message

Petterson Mikael

unread,
Jan 21, 2002, 8:18:05 AM1/21/02
to
Hi,

I am using solaris 8.


I need to check if a certain user group exists and if it does it has to
be removed.
Next step is to create a group.
Has anyone done this in a shell script?

//Mikael

Gabriel Krabbe

unread,
Jan 21, 2002, 10:35:07 AM1/21/02
to
On Mon, 21 Jan 2002 14:18:05 +0100, Petterson Mikael wrote
regarding 'Check if a group exists':

if grep "^${groupname}:" /etc/groups >/dev/null 2>&1 ; then
groupdel $groupname
fi
groupadd $othergroupname


Cheers,

Gabe
--
A computer, to print out a fact, / Will divide, multiply, and subtract.
But the answer can be / No more than debris
If the input was short of exact,

Tony Curtis

unread,
Jan 21, 2002, 11:58:42 AM1/21/02
to
>> On Mon, 21 Jan 2002 15:35:07 +0000 (UTC),
>> Gabriel Krabbe <gabe+...@ucrc.org> said:

> On Mon, 21 Jan 2002 14:18:05 +0100, Petterson Mikael
> wrote regarding 'Check if a group exists':
>> Hi,
>>
>> I am using solaris 8.
>>
>> I need to check if a certain user group exists and if
>> it does it has to be removed. Next step is to create a
>> group. Has anyone done this in a shell script?

> if grep "^${groupname}:" /etc/groups >/dev/null 2>&1 ;
> then groupdel $groupname fi groupadd $othergroupname

It would be safer to expand this to

if getent group groupname < then ... >

to handle all known repositories for group.

"man getent" for more.

hth
t
--
Oh! I've said too much. Smithers, use the amnesia ray.

Stuart Fuller

unread,
Jan 21, 2002, 12:05:01 PM1/21/02
to
Gabriel Krabbe wrote:

> On Mon, 21 Jan 2002 14:18:05 +0100, Petterson Mikael wrote
> regarding 'Check if a group exists':
>> Hi,
>>
>> I am using solaris 8.
>>
>> I need to check if a certain user group exists and if it does it has to
>> be removed.
>> Next step is to create a group.
>> Has anyone done this in a shell script?
>
> if grep "^${groupname}:" /etc/groups >/dev/null 2>&1 ; then
> groupdel $groupname
> fi
> groupadd $othergroupname
>
>
> Cheers,
>
> Gabe

Well, it's /etc/group, not /etc/groups. Also, to be more complete, you'll
need to check the NIS maps and NIS+ tables to see if the target group is
listed there. Changing that is a little more work.

Stu

Gabriel Krabbe

unread,
Jan 21, 2002, 12:31:31 PM1/21/02
to
On Mon, 21 Jan 2002 17:05:01 GMT, Stuart Fuller <stuf...@usa.net>
wrote regarding 'Re: Check if a group exists':

> Gabriel Krabbe wrote:
>
>> On Mon, 21 Jan 2002 14:18:05 +0100, Petterson Mikael wrote
>> regarding 'Check if a group exists':
>>
>>> I need to check if a certain user group exists and if it does it has to
>>> be removed.
>>> Next step is to create a group.
>>> Has anyone done this in a shell script?
>>
>> if grep "^${groupname}:" /etc/groups >/dev/null 2>&1 ; then
>> groupdel $groupname
>> fi
>> groupadd $othergroupname
>
> Well, it's /etc/group, not /etc/groups.

Sorry, my bad.

> Also, to be more complete, you'll need to check the NIS maps and NIS+
> tables to see if the target group is listed there.

Yes, Tony Curtis already mentioned getent. The lines up there were the
first thing that came to mind; I do find the task of identifying whether
a given group exists to be pretty trivial. See also below.

> Changing that is a little more work.

Oh, sure. Even when you're just living of local files, the "groupdel"
command can fail, e.g. because you're trying to remove a user's primary
group, and that's not handled above. Best course would be to check
only local files (there can be multiple groups of a name in different
databases), and throw a warning if the group name is also present
elsewhere.

Something like:

(untested - probably won't work)
---8<-------
if grep "^${groupname}:" /etc/group >/dev/null 2>&1 ; then
if groupdel $groupname ; then
echo "group deleted locally - if this is a NIS master, remember to update"
else
echo "groupdel failed - aborting." >&2
exit 2
fi
else
echo "group not local"
fi

if getent group $groupname >/dev/null 2>&1 ; then
echo "group still present somewhere - check the master server." >&2
exit 1
fi

echo "group gone."
exit 0
---8<-------


Cheers

Gabe
--
When all else fails, read the manual.

0 new messages