[ale] Read multiple variables

JD jdp at algoloma.com
Thu Sep 12 17:08:19 EDT 2013


Just sayn'.

Trusting the /etc/passwd file to gather this data may not be accurate.  The
system call getpwent() should be used to request information about users ...
that historically ... many years ago ... was only stored in the /etc/passwd
file.  In the days since NIS, NIS+, LDAP and PAM .... well, it is critical to
use that system call if you want robust solutions.

Plus, this only gets the primary group, not all the groups of which a user might
be a member.  For that ...  getgrent() ... there are many more calls avialble
for this sort of stuff.

On 09/12/2013 04:43 PM, Scott Plante wrote:
> Another alternative using just bash, and skipping awk altogether:
> 
> (
>   IFS=:; while read name pw uid gid comment shell;  
>   do    
>     [ "$name" = person ] && echo UID is $uid, GID is $gid  and it\'s $comment; 
>   done < /etc/passwd
> )
> 
> The parens keep IFS localized. You could also save it in another variable and
> reset it instead.
> 
> Not sure if you're trying to match the username, but if so your grep should
> probably be
> grep "^person:" so you don't match that name in another user's comments or part
> of another user name 
> i.e. user login "max" vs "maxwell" or comment "Account for max resources service".
> 
> Scott
> --------------------------------------------------------------------------------
> *From: *"leam hall" <leamhall at gmail.com>
> *To: *"Atlanta Linux Enthusiasts" <ale at ale.org>
> *Sent: *Thursday, September 12, 2013 8:35:05 AM
> *Subject: *[ale] Read multiple variables
> 
> Well, I used to know how to do this. Trying to get multiple variables set in one
> line:
> 
> while read UID GID COMMENT
> do
>  echo UID is $UID, GID is $GID, and it's $COMMENT"
> done < grep person /etc/passwd | awk -F":" '{ print $3, $4, $5 }'
> 
> Does not work well at all. Any ideas?
> 
> Thanks!
> 
> Leam
> 


More information about the Ale mailing list