[TriLUG] Scripting question

Jeremy Portzer jeremyp at pobox.com
Thu Jan 9 16:36:55 EST 2003


On Thu, 2003-01-09 at 16:22, Janyne Kizer wrote:
> I am trying to get a list of home directories for regular users in a
> script.  The problem that I am running into is we have some cases where
> users with similar IDs are on the system.  For example, sa, saclark and
> sange or edware and dware.  On our test system we have emma and emma60. 
> Anyway, I am trying to do something similar to this:
> 
> for userid in `cat /etc/passwd | awk -F":" {'print $1'} | grep -xv
> "^the-stuff-we-don't-want"
> homedir=`grep $userid /etc/passwd | awk -F":"  {'print $6'}`
> 
> and of course it is returning two homedirs for emma (/home/emma and
> /home/emma60).  How can I get it to do an exact match?  From the command
> line this works:
> 
> grep '\<emma\>' /etc/passwd
> 
> but I must be doing something wrong with the escaping when I put it into
> a script.

Why are you grepping for "<emma>" from the command line... what are the
angle brackets for?

Since the username is always at the start of the line, and has a colon
after it, you can use that pattern to match a specific username, ie:
	homedir=`grep ^${userid}: /etc/passwd | awk -F":"  {'print $6'}`

What format is "the-stuff-we-dont-want" in?   A list of usernames?   The
following pipeline should list the homedirectories without a loop at
all:

egrep -v "^(root|adm|daemon|other|bad|users):" /etc/passwd | 
awk -F: '{print $6}'

Hope this helps,

Jeremy





More information about the TriLUG mailing list