Sunday, July 13, 2014

Unix Shell: Merge User Accounts(3)

5. Process dupusers and dupuids
make_old_new_list:
 #! /bin/bash  
   
 rm -f old_new_list  
   
 #Setup the Field Separator  
 old_ifs=$IFS  
 IFS=:  
   
 #Read in the user password and uid  
 #Inside the loop, read in the second line, since  
 #we are reading from dupusers, there are supposed to  
 #be two lines with same user names but different user  
 #ids, otherwise, we put error information to standard  
 #error output  
   
 #Inside the loop, we put the username, first uid, and  
 #second uid to old_new_list. Put ther username, password  
 #and second uid to unique2  
 while read user passwd uid  
 do  
   if read user2 passwd2 uid2  
   then  
     if [ $user = $user2 ]  
     then  
       printf "%s\t%s\t%s\n" $user $uid $uid2 >> old_new_list  
       echo "$user:$passwd:$uid2"  
     else  
       echo "$0: out of sync: $user and $user2" >&2  
       exit 1  
     fi  
   else  
     echo $0: no duplicate for $user >&2  
     exit 1  
   fi  
 done < dupusers > unique2  
 IFS=$old_ifs  
   
 #Count how many records dupuids has, we need to generate  
 #that number of new user ids. By plan, we are going to  
 #generate a new user id for each record in dupuids  
 count=$(wc -l < dupuids)  
   
 #Setup the positional parameters inside the script, then  
 #it would have an array of new ids  
 set -- $(./newuid -c $count unique_ids)  
 IFS=:  
   
 #Read in each record from dupuids, for each record  
 #put in the new uid into unique3  
 while read user passwd uid  
 do  
   newuid=$1  
   shift  
   echo "$user:$passwd:$newuid"  
   printf "%s\t%s\t%s\n" $user $uid $newuid >> old_new_list  
 done < dupuids > unique3  

terminal:
1. Print out the content of file "dupusers"
2. Print out the content of file "dupuids"
3. Run make_old_new_list
4. Print out the file content of "old_new_list"
It combines two "xx3" records in dupusers into one record, and new xx3's userid is 3. And for xx1, and xx2 in dupuids, their user ids are replaced by new user ids
5. Print out the content of unique2, which is from processing dupusers
6. Print out the content of unique3, which is from processing dupuids
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat dupusers  
 xx3:pw3:2  
 xx3:pw3:3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat dupuids  
 xx1:pw1:1  
 xx2:pw2:1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./make_old_new_list  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat old_new_list  
 xx3    2    3  
 xx1    1    5  
 xx2    1    6  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat unique2  
 xx3:pw3:3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat unique3  
 xx1:pw1:5  
 xx2:pw2:6  

6. Combine unique[123] to get the final new user records
terminal:
1) Print out content of u1.passwd
2) Print out content of u2.passwd
3) Print out content of unique1, which includes users who have both same usernames and same userids in both u1.passwd and u2.passwd, or users whose user names and user ids only exist in one password file but not the other
4) Print out content of unique2, which includes users who have same user names but different user ids in u1.passwd and u2.passwd.
5) Print out content of unique3, which inlcudes users who have different user names but same user ids in u1.passwd and u2.passwd.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat u1.passwd  
 xx:pw1:0  
 xx1:pw1:1  
 xx3:pw3:2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat u2.passwd  
 xx:pw1:0  
 xx2:pw2:1  
 xx3:pw3:3  
 xx4:pw4:4  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat unique1  
 xx4:pw4:4  
 xx:pw1:0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat unique2  
 xx3:pw3:3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat unique3  
 xx1:pw1:5  
 xx2:pw2:6  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ sort -k 3 -t : -n unique[123] >final.passwd  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat final.passwd  
 xx:pw1:0  
 xx3:pw3:3  
 xx4:pw4:4  
 xx1:pw1:5  
 xx2:pw2:6  

No comments:

Post a Comment