[TriLUG] Scripting question

Stephen P. Schaefer sschaefer at acm.org
Thu Jan 9 16:46:08 EST 2003


Ahh, so you want it in a variable.  I suggest this might be more 
reliable than your proposed solution:

homedir=`awk -F":" "/^${userid}:/ { print \\$6 }" /etc/passwd`

I'm worried that your \<$userid\> might match something in the GECOS 
field for the wrong account.  The regular expression I propose will only 
  match at the beginning of the password line, and will match the entire 
userid in passwd (since : is an illegal character in a userid).

     - Stephen

Janyne Kizer wrote:
> Now I wish that I had posted sooner.  I think that I have it working
> now.  Isn't that always the case?  Right after I ask for help I have an
> "ah ha!" moment...
> 
> homedir=`grep "\<$userid\>" /etc/passwd | awk -F":"  {'print $6'}`
> 
> Thanks for listening!
> 
> 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.
>>
>>Any suggestions would be appreciated!
>>--
>>
>>Janyne Kizer
>>Systems Programmer Administrator
>>NC State University, College of Agriculture & Life Sciences
>>Extension Information Technology
>>_______________________________________________
>>TriLUG mailing list
>>    http://www.trilug.org/mailman/listinfo/trilug
>>TriLUG Organizational FAQ:
>>    http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
> 
> 





More information about the TriLUG mailing list