Is getent a better option than parsing the /etc/passwd file through while loop
Dear All,
I am trying to fetch the user records like: SHELL COMMENTS HOME_DIR PrimaryGroup Secondarygrouplist from /etc/passwd file and display in a format. I have written following script Code:
for __NAME__ in `awk -F: '{printf " "$1}' /etc/passwd` Is there any other better way to accomplish this? |
What you do seems quite inefficient (and also you put ` wrong).
Code:
USID=`getent passwd $__NAME__` | cut -d: -f3 Now, you iterate over all entries in /etc/passwd. I'd do it with Code:
( while read; do |
Quote:
Sorry, I have pasted quite old code. Code:
for __NAME__ in `awk -F: '{printf " "$1}' /etc/passwd`;do Quote:
But In my above pasted code I will get all usernames at once through awk -F: '{printf " "$1}' /etc/passwd And I use getent to find the details of user which internally finds it through administrative databases like passwd and shadow. Instead of passing /etc/passwd through while loop. |
In the beginning I would recommend also getting "groups" entry into a variable, and parse the line taken from variable then.
Let us now try to compare what actually happens. I suggest reading each passwd line once, and parse it in memory. You say that retrieving one field a time is better. Unfortunately, what actually happens is that the first awk invocation will read entire /etc/passwd file. Also, getent will read entire /etc/passwd pefix until the needed line - and you retrieve the same line multiple times. Also your code spawns more processes. By the way, on my machine the only reasonable way to get user's primary group is using passwd entry and then getting an entry from /etc/group file (here getent will fit nice). "groups" just prints all groups the user is in without obvious emphasis for primary group. |
Thanks for your inputs raskin.
I have tried in a different way now. mostly this could be faster... Code:
awk -F: '{printf " RESULT_START __NAME__:"$1":USID:"$3":COMMENTS:"$5":HOME_DIR:"$6":SHELL:"$7" RESULT_END\n"}' /etc/passwd I have to execute system() function of awk here to execute groups command. Can you please help me with this ? |
Well, I don't use awk much (and so I have never seen system() function).. Actually, is it just so time-critical code to optimize it beyond removing obvious problems?
|
Quote:
|
I guess spawning a couple of processes per line is not too fast, too. I guess writing it all in a compiled language is what you need if it is really time-critical. Also, you'd better parse the files linearly on load (you need all entries anyway) and put results in a data structure more suitable for search than a linear list.
|
Well here are a couple of alternatives:
bash: Code:
#!/bin/bash Code:
#!/usr/bin/awk -f Code:
./script /etc/passwd |
Assigning variables in awk
Thanks for your reply grail..
Will try this once I get back to my linux environment |
Thanks a lot grail...Both the scripts are working great...can you suggest me any good awk turotial.
I am trying to understand your script.. Thanks again to both grail and raskin... |
I normally just follow this -> http://www.gnu.org/manual/gawk/html_node/index.html
If you have any questions I am happy to help :) |
Quote:
Yes, I have doubts in 2nd script Code:
if(sec ~ /./) Code:
if(grps ~ "."$1) |
Yes I can see how that is a little confusing, so here we go:
sec variable - this is to store the secondary groups that a user "may" be a part of, remembering that you do not have to have more than your primary group. so to explain: Quote:
grps variable - the variable stores each line one at a time from the file /etc/group (this part is easy) so to explain: Quote:
As an example, my username is grail so the following line shows me as a member of the following group: Code:
dialout:x:20:grail However the following group is not secondary: Code:
grail:x:1000: As of typing this I have realised the logic is slightly floored because if you put all users into the primary group of 'users' this will show them as both a primary and secondary of the group 'users' ... :banghead: Oh well ... something to play with :) |
Thanks grail...You explained it very well..
I learnt a lot about awk today from you...I have written the script in different way.. Please shed your views on this.. For me it looks very optimized as it does not require any loop execution.. Code:
|
All times are GMT -5. The time now is 03:55 AM. |