[Eug-lug] Strange shell behavior
Ben Barrett
stircrazyben at gmail.com
Sun Dec 31 14:03:33 PST 2006
If any of the filenames (or cmdline input) have spaces, you'll need to be
careful to handle that correctly...
you might want to incorporate xargs, but I am forgetting a specific hint for
this scenario
which is tucked away too deeply, sorry.
Ben
On 12/31/06, Martin Kelly <aomighty at gmail.com> wrote:
>
> 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 /lib /lost+found /media /mnt /opt /proc /root /sbin /srv
> /sys /tmp /usr /var /vmlinuz maximum input line size */ char
> line[MAXLINE]; /backups /bin /boot /dev /etc /home /initrd /initrd.img
> /lib /lost+found /media /mnt /opt /proc /root /sbin /srv /sys /tmp /usr
> /var /vmlinuz current input line */ char longest[MAXLINE]; /backups /bin
> /boot /dev /etc /home /initrd /initrd.img /lib /lost+found /media /mnt
> /opt /proc /root /sbin /srv /sys /tmp /usr /var /vmlinuz longest line
> saved here */ while ((len =3D getline(line, MAXLINE)) > 0)
>
> 1-17.c:
>
> -------------------------------------------------------------------------=
-------
> #define MAXLINE 1000 char line[MAXLINE]; while ((len =3D getline(line,
> MAXLINE)) > 0) {
>
> ...
>
>
> Here's the source for dirgrep:
>
> #!/bin/bash
>
> PWD=3D`pwd`
> cd $1
> for file in $(ls); do
> TEXT=3D`cat $file | grep $2`
> if [ $? -eq 0 ]; then
> echo
> echo "$file:"
> echo
>
> "------------------------------------------------------------------------=
--------"
> echo $TEXT
> fi
> done
>
> cd $PWD
>
>
> Thanks all,
> Martin
> _______________________________________________
> EUGLUG mailing list
> euglug at euglug.org
> http://www.euglug.org/mailman/listinfo/euglug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://euglug.org/pipermail/euglug/attachments/20061231/697ed2b4/attac=
hment.htm
More information about the EUGLUG
mailing list