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