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