None of the filenames in the example I used have spaces though.
However, I think you helped me find a bug that will appear later. cat $file is now cat "$file" so it will interpret spaces literally.
Still having the problem. Anyone else have ideas? Ben Barrett wrote:
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 scenariowhich is tucked away too deeply, sorry. BenOn 12/31/06, *Martin Kelly* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> 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: [EMAIL PROTECTED]:~$ 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 = getline(line, MAXLINE)) > 0) 1-17.c: -------------------------------------------------------------------------------- #define MAXLINE 1000 char line[MAXLINE]; while ((len = getline(line, MAXLINE)) > 0) { ... Here's the source for dirgrep: #!/bin/bash PWD=`pwd` cd $1 for file in $(ls); do TEXT=`cat $file | grep $2` if [ $? -eq 0 ]; then echo echo "$file:" echo "--------------------------------------------------------------------------------" echo $TEXT fi done cd $PWD Thanks all, Martin _______________________________________________ EUGLUG mailing list [email protected] <mailto:[email protected]> http://www.euglug.org/mailman/listinfo/euglug <http://www.euglug.org/mailman/listinfo/euglug> ------------------------------------------------------------------------ _______________________________________________ EUGLUG mailing list [email protected] http://www.euglug.org/mailman/listinfo/euglug
_______________________________________________ EUGLUG mailing list [email protected] http://www.euglug.org/mailman/listinfo/euglug
