euglug.org

[Eug-lug] Strange shell behavior

작성자

카테고리:



horstSun Dec 31 15:01:08 PST 2006

Have you considered the use of 'find' , e.g.:
  find . -type f -name 'your*here' -exec grep 'string options pattern ...' /dev/null {} \;

  a) /dev/null is there to make grep list the filename (because your term is found in {}, not in /dev/null
  b) -type f  excluded directories, symlinks, sockets, ...
     (grep on directories may 'report' binary stuff which can confuse your terminal or other 'things' downstream)
  c) if your serach terms are simple text 'fgrep' may be better, not just because it's faster -- it saves you a lot of \escaping.
  d) find let's you define you much other handy stuff, ctime, atime, inode,...

I didn't look too closely at your script, but it seemed like you'r not 
testing file type.

Sorry for the quick&sloppy response.

Happy New Year to all........................Horst


> Date: Sun, 31 Dec 2006 13:33:03 -0800
> From: Martin Kelly <aomighty at gmail.com>
> Reply-To: Eugene Unix and Gnu/Linux User Group <euglug at euglug.org>
> To: Eugene GNU/Linux Users Group <euglug at euglug.org>
> Subject: [Eug-lug] Strange shell behavior
> 
> I have just written a shell script that greps a directory for a certain 
> pattern and reports each file that contains the pattern along with the 
> filename before it (this is why I wrote it... if I just do a "ls | cat | grep 
> pattern" or something like that it will report the text that matches but not 
> the filename).
>
> I am getting strange behavior in that it works, but sometimes it randomly 
> reports the contents of my / directory. This does not always happen, only 
> sometimes.
>
> Here's an example:
> martin at home:~$ misc/dirgrep code/c/kr/1/ MAXLINE
>
> 1-16.c:
> --------------------------------------------------------------------------------
> #define MAXLINE 1000 /backups /bin /boot /dev /etc /home /initrd /initrd.img
  ...etc...
>