[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Which tool, and how, to get partial string from file?



Thomas Adam wrote:

On Fri, Aug 20, 2004 at 09:46:52PM -0500, Kent West wrote:
Maybe I'm asking the wrong question.

Here's a new question.

How can I test to see if the word "tuber" is in the /etc/passwd file, reliably, and take an action if it is, and take a different action if it's not, in a bash shell script?

You won't need to read the file, use "getent passwd":

getent passwd | grep tuber

If 'tuber' is the username, you can shorten this to:

getent passwd tuber

or even:

[ -n "$(getent passwd tuber)" ] && tuber exists || tuber does
not exist
kowari:~# getent passwd john
kowari:~# getent passwd summer
summer:x:1001:1001:John Summerfield:/home/users/summer:/bin/bash
kowari:~# getent passwd John
kowari:~#

OTOH

kowari:~# grep -qiw john /etc/passwd && echo Found John Found John
kowari:~# grep -qiw jon /etc/passwd && echo Found John
kowari:~#

For more, man grep

--

Cheers
John

-- spambait
1aaaaaaa@computerdatasafe.com.au  Z1aaaaaaa@computerdatasafe.com.au
Tourist pics http://portgeographe.environmentaldisasters.cds.merseine.nu/



Reply to: