Finding duplicate users on CentOS

I'm using the following code to find the duplicate username. However, it gives an error.

#!/bin/bash cat /etc/passwd | cut -f1 -d":" | /bin/sort -n | /usr/bin/uniq -c |\     while read x; do [ -z "${x}" ] && break set - $x         if [ $1 -gt 1 ]; then             uids=`/bin/gawk -F: '($1 == n) { print $3 }' n=$2 \             /etc/passwd | xargs`             echo "Duplicate User Name ($2): ${uids}"         fi     done 

I'm facing a syntax error near the token 'done' and numberic error. How can I fix this error?


$ cut -d: -f1 /etc/passwd | sort | uniq -d

This will extract the first field (the usernames) of the :-delimited /etc/passwd-file, sort the result and report any duplicates.

To also get the UID and the rest of the duplicated passwd entries:

cut -d: -f1 /etc/passwd | sort | uniq -d |
while read -r username; do
  grep "^$username:" /etc/passwd

To only get the duplicate usernames and their UID:

cut -d: -f1 /etc/passwd | sort | uniq -d |
while read -r username; do
  awk -F: -vu="$username" '$1 == u { print $1, $3 }' /etc/passwd

A short note on your script. The syntax looks mostly ok, but you need ; after break and there is a space after both \ (this may be a cut-and-paste error). Also, I'd avoid giving full paths to standard utilities if there is no good reason for it, and the awk program does not require GNU awk so just awk will do.

  1. You need to have a do somewhere between the while and the done — typically, right after the read, or after the check that you got data.
  2. set - $x should be on a line by itself, or at least separated from the break with a semicolon (;). (This is probably a good place to put the do.)


  1. Rather than doing set - $x, consider changing read x to read count name.
  2. Just for clarity, you might want to change `…` to $(…) — see this, this, and this.

I would do something like that with awk (one-liner):

awk -F: '{if ($1 in users) print "Duplicate Username: "$1 ; else users[$1]}' /etc/passwd

find username in users array variable, if duplicate print msg else add the user to the array

Category: centos Time: 2016-07-31 Views: 1
Tags: centos

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.131 (s). 12 q(s)