I was in an interview and another interview walked in and said “Hi, do you know the groups tool in unix?” At which point i said “Yes”. At which point the interview hands me a whiteboard marker and says “Great. Write it”. And after a couple of minutes of thinking about how to do it, I wrote it down.
Now, I didn't end up doing it on a whiteboard, I did it on a piece of paper I had, which I kept, but unfortunately I have not beed able to scan it for everyone's reading enjoyment. But I'll do that in the future.
So once I got home after the interview, I took the algorithm, and applied proper PERL syntax. And here is the code:
#!/usr/bin/perl ####################################################################### # Created By: Bryce Verdier # on 1/19/10 # Function: rework the output of the unix groups command # NOTE: FOR USE ON UNIX MACHINES! ####################################################################### use warnings; use strict; use Getopt::Std; our($opt_u); getopts('u:'); #Get the results from /etc/groups my $groups= `grep -w $opt_u /etc/group`; #split it into an array for per line processing my @split_groups= split(/\n/, $groups); #output the username we're looking for ( to emulate groups better) print ("$opt_u : "); #where all the magic really happens #split the array again, and output the first part of the split, as based #on the strcuture of the /etc/groups file foreach (@split_groups){ my @temp_split= split(/:/, $_); print ("$temp_split[0] "); } #clean up print ("\n");
So after writing the code above originally, the interviewer looks things over and asks me a couple of questions. I don't remember all of them, the only one I can remember now ( almost two weeks later ) is below:
“Why are you using calling out instead of using a system call?”
After looking around, PERL's systems calls abilities are fairly minimal. I could open a file, and parse through it. But I figured why not save myself some of the work and use the tools in the shell to ease my work.
Regardless, if you are in preparing for an interview maybe this might give you a leg up. Or at least now other interviewers will have to start using a different question.
Hey Dave, Thanks for posting
Hey Dave,
Thanks for posting again!
In a way you are right, my code is not an exact duplicate of the standard "groups" command. And while the program is not exact, it does in fact display the default group... just as its first entry.
Now I admit that my test system for this is my own machine, so my testing environment is limited. (standard fedora 12 install with one user account added [my own]).
Here is an excerpt from my /etc/groups file:
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,admn
disk:x:6:root
wheel:x:10:root,bryce
bryce:x:500:
so when I run groups bryce and root here is my output:
bryce : bryce wheel
root : root bin daemon sys adm disk wheel
But when I run my own code here is my output:
bryce : wheel bryce
root : root bin daemon sys adm disk wheel
So after looking a little more in depth at the results of the code, I can see the difference between my program and the standard tool. My program's output is based on the order of the groups file, the standard tool is not.
Again Dave, thanks for the comment.
Actually, that's not what I
Actually, that's not what I was talking about. Maybe this will be more illustrative:
[dave@hostname ~]$ grep dave /etc/passwd | awk -F: '{print $4}'
10
[dave@hostname ~]$ grep -w 10 /etc/group
wheel:x:10:root
[dave@hostname ~]$ grep dave /etc/group
apache:x:48:dave
named:x:25:dave
[dave@hostname ~]$ groups dave
dave : wheel apache named
This is on Fedora.
Cheers,
-Dave
Hey Dave, Thank you for
Hey Dave,
Thank you for posting that. I now understand the problem you were trying to tell me of before. I will modify the script to take that into account.
Thank you again Dave; I greatly appreciate it!
This does not cover their
This does not cover their default GID, which the real groups command does.
Post new comment