Posts Tagged finger

Using finger with awk, finding out lab account info of your teammates in case you forgot

On one evening during my third year at U of T, I was working on an CSC343(intro to databases) assignment with my partner. When the assignment was finally completed I was overjoyed, with the assignment due in less than an hour, my partner put me in charge of submitting the assignment as he had to leave immediately and run some errands. When I was ready to submit the assignment electronically on a online app, the app asked me for my partner’s lab account name. I was in total panick, because I forgot his account name! I took a DEEP breath and tried to call my partner on his cell phone, but he didn’t pick up. “FML, my partner’s gonna kille me” I thought . I then messaged a friend and asked for consultation and possibly prayers. Instead of prayers however, she gave me a bash shell script that uses for loops, finger, /etc/pwd and grep. The script was magix, because it located my partner’s account info, and to this day, I still think she is l33t.

Now a year later, I wondered if there were a simpler way to find out the account information of a classmate by name (or part of his/her name) without using shell scripts. And here is what I came up with

export $REGEX=<name>
cat /etc/passwd | awk -F : '{print $1}' |xargs finger -l|awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

The command might look daunting at first, but if you break it down, it is really simple.
cat /etc/passwd displays various information of all the user accounts on system, it usually looks like this

riak:x:114:123:Riak Data Store,,,:/var/lib/riak:/bin/bash
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false

for more information on /etc/passwod visit here

awk -F : {print $1}

The above command basically says that for each line, print the first ($1) element delimited by the symbol “:”. In our case it will display all of the user account name.


The above would be an example of user accounts.

xargs finger -l

finger allows us to find more information on a given account name, including the user’s name. This is pretty essential.

awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

prints the line that contain the values that are defined by $REGEX.

So this little trick command allows the user to find account information based on the user’s registered user name, I hope this is useful to you, and I hope my WTF moment doesn’t happen to you.



Leave a comment