script to change group from a file having list of users

In this post we are sharing the script to change group from a file having list of users. You can do the changes in script as per your requirement.
The script is also using the /etc/group file to validate group names which could be existing in user supplied group file.

Given below is the sample script.

#!/bin/bash

##########################################################
#This script is made to change the group id ##
# Author: Sharad Kumar Chhetri ##
# Blog: https://sharadchhetri.com
# Date of Creation: 17-Jun-2011
# Version: 1.1
##########################################################
echo "Note: User list file and the script must be in same dir and should be run from same dir"
echo ""
echo "Currently You are in: `pwd`"
echo "list of files in this dir: `ls -l`"

echo "Give the name of file which has Users Name"
read userfile
echo "You have selected the file called $userfile"

echo "Give the Group Name in which user will be added"
read grpname
grpcheck=`grep $grpname /etc/group|cut -d: -f1`

echo $grpcheck
if [ -f $userfile ];then
echo "$userfile file is found"
else
echo "$userfile file not found"
echo "U r going to exit in 2seconds"
sleep 2
exit
fi

if [ $grpname == $grpcheck ];then
echo "$grpname group is found"
else
echo "$grpname group is not found"
echo "U r going to exit in 2seconds"
sleep 2
exit
fi

echo "Do u want to continue (Y/N)"
read xyz
case $xyz in
Y|y) for i in `cat $userfile`;do usermod -g $grpname $i;done;;
[Yy][Ee][Ss]) for i in `cat $userfile`;do usermod -g $grpname $i;done;;
N|n) exit;;
[Nn][Oo]) exit;;
*) exit;;
esac

echo "Status After Running the Script-- Check UserID and GroupID"
for i in `cat $userfile`
do id $i
done

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.