SUMMARY: a question of "find" command

From: Carolyn Lee <carolynlee1999_at_hotmail.com>
Date: Mon May 13 2002 - 12:14:18 EDT
Hi Gurus,

Sorry for this a little late summary. Thank you all who responses for your 
help! I got more than 60 responses, I apologize I am not able to list all 
names who replied.

Quite a few people pointed out that I missed a "\{};" in the script. That is 
not the right answer to my question. Actually I knew that but I missed it 
when I send the request out.

i.e., the following commands which quite a few people suggested to try did 
not work if there are multi entries. The shell is expanding the $1 as it 
gets passed:

find . -name $1 -exec grep $2 {} \; -print
find . -name "$1" -exec grep "$2" {} \; -print
find . -name $1 -print | xargs grep $2


Larye D. Parkins pointed to the right direction. The following is from his 
message, that solved my problem:
--------------------------
find . -name \'$1\' -exec grep $2 {} \;

The escaped single quotes are needed to permit variable expansion by the 
shell for $1, but pass the "*" form (since in your example, $1 contains an 
escaped wild card) to 'find' without reexpansion: i.e, the shell will 
execute the find command as if you typed

  find . -name '*.h' ... at a shell prompt.

The results of the find are passed one at a time to the -exec target via the 
"{}", and you need the escaped semicolon to end the exec string.  If you 
need to know the file name in which the string was found, you need to use

        find . -name \'$1\' -exec grep -l $2 {} \;

which will output (example):
      signal.h
indicating the grep string was found in this file.

If you use
        find . -name \'$1\' -print -exec grep $2 {} \;
it will output (example):
        foo.h
        bar.h
        signal.h
        #define SIG_IGN   ...
        baz.h
which shows the names of all the files that did NOT contain the string, plus 
the strings found, after the file name(s) in which they were found
-----------------------------------------

Karl Vogel and Lee Trujillo sent me the user-friendly shell script, I tested 
it and it worked. I can send you that if you are interested in that.

Thanks again to all who replied,
- Carolyn -

>Hi Gurus,
>
>It might be a simple question, but I do not know how to do the trick.
>
>I want to write a shell script to use 'find' command to find some files and
>do a 'grep' on it, it works like this way:
># myfind "*.h" SIG_IGN
>
>The following is my script:
>#! /bin/sh
>/usr/bin/find . -name $1 -exec grep $2 -print
>
>It does not work, how can I do this?
>
>TIA for your help.
>


_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Mon May 13 12:20:37 2002

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:42:42 EST