Re: script question (165 Views)
Reply
Super Advisor
navin
Posts: 702
Registered: ‎11-15-2001
Message 1 of 7 (165 Views)
Accepted Solution

script question

Hello ,
i have 2 files called users and /etc/passwd.
I need to grep the users from /etc/passwd .
I'm using the below script ,...what is wrong.please advice
for i in $users ; do
>grep $i /etc/passwd >/tmp/users1
>done

Thanks much
Learning ...
Please use plain text.
Trusted Contributor
Juan M Leon
Posts: 285
Registered: ‎01-14-2003
Message 2 of 7 (165 Views)

Re: script question

Navin,

You need a small modification in your script

for i in `cat users` ; do
grep $i /etc/passwd >> /tmp/users1
done


"cat users " I assume that the file is users and it has one field per record with the UID

">>" You need to put double redirect. otherwise you will only have the last successful record in the users1 file.
>> means add at the end where > means clear and add in this file.

good luck
Please use plain text.
Trusted Contributor
Juan M Leon
Posts: 285
Registered: ‎01-14-2003
Message 3 of 7 (165 Views)

Re: script question

Note : Please dont forget to assign points.
Thank you
Please use plain text.
Honored Contributor
Geoff Wild
Posts: 7,168
Registered: ‎06-04-2001
Message 4 of 7 (165 Views)

Re: script question

Or sans cat:

for i in $(grep $i /etc/passwd >/tmp/users1
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 5 of 7 (165 Views)

Re: script question

To print all users that have an entry in the "users file" and a corresponding entry in the /etc/passwd file use the following:

# grep -f /etc/passwd
Please use plain text.
Trusted Contributor
Juan M Leon
Posts: 285
Registered: ‎01-14-2003
Message 6 of 7 (165 Views)

Re: script question

Sandman,

I like your oneline script.
very simple... =)
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,752
Registered: ‎06-21-2000
Message 7 of 7 (165 Views)

Re: script question

The problem with the ways mentioned is that they will look for ALL occurrences of whatever is in the users file.

Say you have a file like:

tom
dick
harry

When you grep as the folks have done above you could get the users:

tom
tomsmit
dick
dickjr
harry
harry2

It would also be possible to gets matches in other areas of each line, like the home directory or GECOS field.

If you want to look for specific user names only you could do:

for i in $( grep ^${i}: /etc/passwd >/tmp/users1
done

The '^${i}:' will look for ${i} ONLY at the beginning of each line with a ':' after it. So it would show only:

tom:*:UID:GID:GECOS:homedir:shell
dick:*:UID:GID:GECOS:homedir:shell
harry:*:UID:GID:GECOS:homedir:shell
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation