How to check if a group exists and add if it doesn't in Linux Shell Script
LinuxShellUbuntuLinux Problem Overview
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.
Linux Solutions
Solution 1 - Linux
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
Solution 2 - Linux
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
Solution 3 - Linux
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
Solution 4 - Linux
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.
Solution 5 - Linux
Single line:
$getent group <-groupname-> || groupadd <-groupname->
Solution 6 - Linux
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
Solution 7 - Linux
$ 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
Solution 8 - Linux
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