The question is published on by Tutorial Guruji team.
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 numeric error. How can I fix this error?
Answer
$ 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 done
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 done
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 (now removed by an edit)). 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.