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?