Search code examples
bashcomparison

How to check if a UID is unique using Bash?


I'm trying to do a simple bash Script to check if there is a not unique UID in /etc/passwd.
I did a script already to display all the UID but I can't figure out how to compare them and return the not unique one if it exists.

Here is the script I wrote:

#!/bin/bash
passwd="$(cat /etc/passwd)"
while read -r line; do
    IFS=':' read -ra arrayPasswd <<< "$line"
    echo ${arrayPasswd[2]}
done <<< "$passwd"

How could I compare them and return the not unique one in case there is one ?


Solution

  • If you have a script that outputs all of your UIDs, you can use sort and uniq programs to get the desired result:

    $ your-script | sort | uniq -d

    uniq program takes some lines and deletes consecutive repeats. Flag -d makes it output only duplicates and delete unique ones. You need to use sort first to make any repetitions consecutive.