Save awk record field in bourne shell variable

Hello,

I am trying to write a shell script that maintains the health of the passwd file. The goal is to check for duplicate usernames, UID's etc. I am able to find and sort out the UID and login names via awk (which I would like to use), but I can't figure out how to save the record field into a `sh` variable so I can use it for further testing. The algorithm I think of is:

  1. Read in passwd file
  2. determine field seperator
  3. save the UID field ($3) in shell variable.
  4. continue and save the next UID field in another shell variable
  5. compare the two UID's
  6. if the same print user notification
  7. else, continue
echo "Check for root uid..."
 16 awk -F: '$1 ~/root/ {print "Found Root ID"}' ~/script/passwd
 17 
 18 echo "Check for duplicate user id's..."
 19 counter=0
 20 for line in  ~/script/passwd; do
 21         uidcurrent=`awk -F: '$3' ~/script/passwd`
 22         if [ $counter -eq 0 ]; then
 23                 counter=`expr $counter+1`
 24                 uidprevious=$uidcurrent
 25                 continue
 26         elif [ $uidprevious -eq $uidcurrent ]; then
 27                 echo "Duplicate UID found"
 28         else
 29                 echo "..."
 30                 uidprevious=$uidcurrent
 31         fi
 32 done
 33 echo "Duplicate UID check done"
 34 echo "Check for duplicate login names..."
 35 exit 0

I appreciate your time and effort.

-Daniel

An awk way to get duplicate uids and usernames. Most of the other fields could be harmless when duplicated, like the gid. Also some fields can be okay and empty.

awk -F':'  ' uname[$1]++ && uname[$1]>1 {print "duplicate user:", $1} 
               uid[$2]++ && uid[$2]>1 {print "duplicate uid:", $2} ' /etc/passwd