linuxbashunixsearchetcpasswd

See who is currently logged in and get their information from /etc/passwd


I wanted to be able to see who is currently logged in into the server and then search /etc/passwd file based on their username and find their ID (column3) and full name (column5) and display them together.

For example:

$ who    
USER         TTY
billyt     pts/2 
$ cat /etc/passwd
…
billyt:x:10:100:Tom Billy:/home/billyt:/bin/bash
…

My output should display his username, ID, and full name:

Username: billyt   ID: 10   FullName: Tom Billy

This is what I tried so far:

#!/bin/bash
file="/etc/passwd"

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
do
        # display fields using f1, f2,..,f7
        echo "Username: $f1, UserID: $f3, FullName: $f5"
done <"$file"

I tried displaying fields I needed (f1, f3 and f5). Is this a good approach? Will I be able to simply search from who command, save the first field of who (username) to who.txt then search it from the file above?


Solution

  • You can store the users in an array from the who command and then while reading the /etc/passwd file, loop through the array to see if the user is present in the array and if so, print the entries from /etc/passwd file in the format you desire.

    Something like:

    #!/bin/bash
    
    while read -r user throw_away; do 
        users+=( "$user" )
    done < <(who)
    
    while IFS=: read -r f1 f2 f3 f4 f5 f6; do
        for name in "${users[@]}"; do
            if [[ "$name" == "$f1" ]]; then
                echo "Username: $f1, UserID: $f3, FullName: $f5"
            fi
        done
    done < /etc/passwd 
    

    We use process substitution <(..) to pass the output of who to first while loop and create an array users. Since we only need the name we use a dummy variable throwaway to capture everything else.

    In the second while loop (I re-used most of your existing code), we check if the first field from `/etc/passwd/ file is present in our array. If so, we print the line in your desired format.

    You can also also use associative arrays (bash v4.0 or later) to store users as keys. I will leave that to you for practice.