The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing the values to the secondary script when it invoked by primary script venu_eie UNIX for Advanced & Expert Users 2 07-03-2008 08:10 AM
Passing the values to the secondary script when it invoked by primary script venu_eie Shell Programming and Scripting 1 07-03-2008 07:16 AM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 03:50 AM
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 03:31 PM
Shell Script: want to insert values in database when update script runs ring Shell Programming and Scripting 1 10-25-2007 04:06 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 07-03-2008
garlic_punisher garlic_punisher is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Need help with this script

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
 

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0