Wednesday, October 17, 2007

ChangeGID.sh

#!/bin/bash
# Change the GID of a Group and make it effective for all the members in /etc/passwd
pwfile=/etc/passwd
grpfile=/etc/group
DATE=`date +%d.%b.%Y`
TIME=`date +%H.%M.%S`


echo Creating backups $pwfile.$DATE.$TIME and $grpfile.$DATE.$TIME
cat $pwfile > $pwfile.$DATE.$TIME
cat $grpfile > $grpfile.$DATE.$TIME

echo Enter group name
read gname
cgid=`grep $gname $grpfile | cut -f3 -d:`
username_s=`cat $pwfile | grep $cgid | cut -f1 -d:`
echo Following users will be affected
echo "##############"
echo $username_s
echo "##############"


echo Enter new GID
read ngid

## Change the GID of the group
groupmod -g $ngid $gname

## Change GID(s) in passwd file

uidc=`grep $cgid $pwfile | cut -f3 -d: | wc -l`
uidh=1
while [ "$uidc" -ge "$uidh" ]
do
{
id=`head -$uidh uid | tail -1`
sed 's/'$id':'$cgid'/'$id':'$ngid'/g' $pwfile > $pwfile.new
cat $pwfile.new > $pwfile
uidh=$[$uidh + 1 ]
}
done

echo The entries in replaced File are
grep $ngid $pwfile.new
echo The original has been backed up as $pwfile.$DATE
echo Thank you
cd $PWD
exit 0

No comments: