I am passing 2 parms to a bash script. The first parm is group and the second parm is the groupID.
I want to write a bash script that will check if group is present in /etc/group
or not.
If not present, then the script should add group and gid to /etc/group
.
If present then it should match the gid with the 2nd param. If gid doesn't match the 2nd param then it should overwrite the gid with 2nd parm.
In short, group name and gid that I am passing to the script, should be in the /etc/group file.
I am running the command as:
./addgroup.sh groupa 123
suppose /etc/group
has an entry:
groupa:x:345
After the running the command, when i browse /etc/group
it should have value
groupa:x:123
I have written the following addgroup.sh
so far:
if grep -q "^$1:" /etc/group
then
echo "group $1 exists:SUCCESS"
else
grep -q "^$1:" /etc/group || /bin/echo "$1:x:$2:" >> /etc/group
cat /etc/group
echo "group $1 added:SUCCESS"
fi
I believe that this code meets your needs and is simpler:
The question states that you would like to update the group ID even if the group already exists. The code here has that feature.
How it works:
grep -v "^$1:" /etc/group
This removes the existing definition of group
$1
if one exists.echo "$1:x:$2:"
This adds back a correct definition of group
$1
with the correct group ID.{ ... } &&
mv /etc/group.tmp /etc/group
If commands in braces executed successfully, this updates /etc/group.
Preserving existing group members
As sjnarv points out, one might want to preserve existing group members. In that case:
This requires GNU awk. (On Ubuntu, GNU awk is available but it is not usually the default.) For standard awk:
How it works:
-v g="$1" -v id="$2"
This uses shell variables
$1
and$2
to define two awk variablesg
andid
.-F:
This tells awk that our field separator is
:
.-i inplace
This tells awk to modify the file in-place (GNU awk only).
$1 == g{save=$4; next}
If we encounter an existing definition of group
g
in the file, save the group members (the fourth field) in variablesave
and then skip the rest of the commands and jump to thenext
line.Here, inside the awk script,
$1
and$4
are awk variables representing the first and fourth fields on the line. These variables are entirely separate and unrelated to the shell variables$
and$4
which would represent arguments to the shell script.print
For all other lines in
/etc/group
, we just print them as is.ENDFILE{print g, "x", id, save}
After reaching the end of
/etc/group
, we print the new definition of groupg
with group IDid
and group memberssave
. (For standard awk, we use END in place of ENDFILE.)OFS=:
This tells awk to use
:
as the field separator on output.