Hi,
I'm new to unix shell scripting so I need some help here. I've been asked to create a script for work that will synchronize the user ids and group ids in the /etc/passwd and /etc/group files with other machines. I know that this is playing with fire a little but it is still required. When I run the following script without sudo then it doesn't execute the -add and -mod commands, but when I run it with sudo (i.e. sudo ./script) then I get the following errors:
[: 30: ==: unexpected operator
./script: 30: groupnum++: not found
for the first loop and
[: 63: ==: unexpected operator
./script: 63: usernum++: not found
for the second loop.
I would NOT suggest running the following script (though I don't think it can do any harm if you use your current passwd and group files as input and input2), but if anyone could point out any errors or offer any ideas it would be greatly appreciated. input and input2 are hypothetically the passwd and group files copied from another machine:
Code:
# Make backups of the files
cp /etc/passwd backuppasswd
cp /etc/group backupgroup
# Variables for the 2 input files
inputpasswd=input
inputgroup=input2
awk -F: '{print $1}' $inputgroup > groupnames
awk -F: '{print $3}' $inputgroup > groupids
groupnum=1
# Add and update groups
while read groupline; do
echo "$groupline"
othergroupid=`sed -n "$groupnum{p;g;}" groupids`
groupid=`awk -F: '$1=="'"$groupline"'" {print $3}' /etc/group`
if [ "$groupid" == "" ]; then
echo "Need to Add Group"
groupadd -g $otherid $groupline
fi
((groupnum++));
if [ "$groupid" != "$othergroupid" ]; then
echo "Group ID Mismatch"
groupmod -g $othergroupid $groupline
fi
# echo Changing Group File and Directory Ownership
# find / -group $groupid | xargs chgrp $othergroupid
done < groupnames
awk -F: '{print $1}' $inputpasswd > usernames
awk -F: '{print $3}' $inputpasswd > ids
awk -F: '{print $4}' $inputpasswd > groups
awk -F: '{print $5}' $inputpasswd > descriptions
usernum=1
#Add and update users
while read userline; do
echo "$userline"
otherid=`sed -n "$usernum{p;g;}" ids`
othergroup=`sed -n "$usernum{p;g;}" groups`
id=`awk -F: '$1=="'"$userline"'" {print $3}' /etc/passwd`
group=`awk -F: '$1=="'"$userline"'" {print $4}' /etc/passwd`
description=`sed -n "$usernum{p;g;}" descriptions`
echo "$description"
if [ "$id" == "" ]; then
echo "Need to Add User"
useradd -g $othergroup -c $description -u $otherid $userline
fi
((usernum++));
if [ "$id" != "$otherid" ]; then
echo "ID Mismatch"
usermod -u $otherid $userline
fi
# echo "Changing User File and Directory Ownership"
# find / -user $id | xargs chown $otherid
# echo "Changing Group File and Directory Ownership"
# find / -user $id | xargs chgrp $othergroup
if [ "$group" != "$othergroup" ]; then
echo "Change User Primary Group"
usermod -g $othergroup $userline
fi
echo
done < usernames
grouplist=`awk -F: '{print $1}' $inputgroup`
#Add users to their appropriate groups
for x in ${grouplist}; do
#Use awk to get the actual comma separated list of users for each group
userlist=`awk -F: '$1=="'"${x}"'" {print $4}' $inputgroup`
arrayuserlist=`echo $userlist | tr ',' ' '`
for y in ${arrayuserlist}; do
echo "Adding to Group"
echo "${x} ${y}"
usermod -a -G ${x} ${y}
done
done
#that section needs some rework so it gets the groups from imported file
rm ./groupnames
rm ./groupids
rm ./usernames
rm ./ids
rm ./groups
rm ./descriptions