Reputation: 747
this is a summary of what i want my code to do:
if (group exists)
then
(add user to group)
else
(create group)
(add user to group)
fi
I am using the Ubuntu virtual machine but all of the results i have found on similar sites do not work.
Upvotes: 71
Views: 123599
Reputation: 6556
If you want to ensure that an existing USER is added to the GROUP then this flow
if (group exists)
then
(add user to group)
else
(create group)
(add user to group)
fi
can be simplified to:
if (group exists)
then
# do nothing
else
(create group)
fi
(add user to group)
then ... # do nothing
can be removed and condition changed to if NOT exists
to simplify the flow further
if (group NOT exists)
then
(create group)
fi
(add user to group)
There is no need to check if (group NOT exists)
if you may attempt to create a group and have no problem if it already exists. Thus the whole flow can be simplified to:
(create group) AND (add user to group)
Upvotes: 0
Reputation: 1152
Grepping /etc/group works, but only on a machine where /etc/nsswitch.conf has:
group: files
meaning that only /etc/group is consulted when determining available groups. Use either of these (by name or by gid):
getent group <groupname>
getent group <groupid>
for a more generic solution, checking the exit status: 0 means "exists", non-zero means "does not exist". For example, to check to see if group 'postgres' exists, and create it if it does not (assuming bash shell, running as a user able to create new groups) run:
/usr/bin/getent group postgres 2>&1 > /dev/null || /usr/sbin/groupadd postgres
Upvotes: 29
Reputation: 2121
$ groupadd --help
Usage: groupadd [options] GROUP
Options:
-f, --force exit successfully if the group already exists,
and cancel -g if the GID is already used
So you can do simply:
groupadd -f some_new_grp
Upvotes: 8
Reputation: 14221
I've found it more useful, to compose andiba's solution into a proper function:
function grpexists {
if [ $(getent group $1) ]; then
echo "group $1 exists."
else
echo "group $1 does not exist."
fi
}
This can for e.g be invoked into your environment by including this function in your /etc/bash.bashrc*
, such that you can then check for the existence of a group, using the following spell:
grpexists group_name
Which should then return one of:
group group_name exists.
or
group group_name does not exist.
Upvotes: 11
Reputation: 3
Geeks great solutions and guidance, thanks for sharing here are my 2 cents to make our lives simpler or lazier :-) I could use to complement an useradd script I have to add several users at once. I'm wondering how it would look like inside a for in loop for several groups: group1, group2, group3...group6 Then useradd to the system something like this?
for g in $( cat fewgroups.txt ); do
groupadd $g
echo "Group:" $g "Exist not added moving on"
else
echo "Group:" $g "added successfully!"
# Then create the users
for u in $( cat 100sofusers.txt ); do
useradd -m -g group1 -G group2,wheel -d /home/$u -c "Just anothe SiFiGeek" -s /bin/bash $u
echo "userID:" $u "added successfully!"
echo $u:$randompw | chpasswd
echo "Password for userID:" $u "changed successfully"
done
Upvotes: 0
Reputation: 166871
Here are 3 commands which should work:
group=sudo
grep -qw ^$group /etc/group || groupadd $group
usermod -aG $group $USER
Or one, when you use -f
/--force
(exit successfully if the group already exists):
groupadd -f mygroup && usermod -aG mygroup $USER
Upvotes: 4
Reputation: 1748
This script may help you:
read -p "enter group name: " group
if grep -q $group /etc/group
then
echo "group exists"
else
echo "group does not exist"
fi
Upvotes: 27
Reputation: 1578
The grep
statement in the solution of rups has some flaws:
E.g. grepping
for a group admin
may return true
("group exists") when there is a group lpadmin
.
Either fix the grep
-query
grep -q -E "^admin:" /etc/group
or use
if [ $(getent group admin) ]; then
echo "group exists."
else
echo "group does not exist."
fi
Upvotes: 111