Click to See Complete Forum and Search --> : help with short script
OhLordy
04-07-2003, 06:20 AM
I am using
(for FILE in `ls -R | grep '.php'`; do grep 'log_event' $FILE; echo $FILE; done) | more
this script but it is not working wuite as expected.
For every file in the lower directory structure it comes up with a grep error saying that the file does not exist.
Can anyone think of a solution?
Thanks Rob
mrBen
04-07-2003, 07:28 AM
I think it may be having problems with your second grep statement, in which case it's one of 2 things:
1. It's not finding the log_event file you refer to.
2. It's not finding $FILE - try running the first ls -R | grep (Etc) command on it's own - do the files come up with fully directory names, or just file names?
OhLordy
04-07-2003, 07:57 AM
The directory names are not coming up with the search so it's looking for the files in the current directory rather than the directory the file is actually in. Can you think of a way around this?
Many thanks.
Rob
chrism01
04-07-2003, 08:15 AM
try the find cmd:
for FILE in `find . -name '*.php' -print 2>/dev/null`
do
grep 'log_event' $FILE
echo $FILE
done
OhLordy
04-07-2003, 11:04 AM
That's brilliant, thankyou. Now, one more thing to make it perfect. How can I make it just print out the ones that do contain this string.
Many thanks
Rob
mrBen
04-07-2003, 11:33 AM
Take out the echo command, and the grep should be printing the results. If that's not working then do grep 'log_event' $FILE && echo $FILE - the logical AND should mean that if the grep fails then the echo will not be performed.
OhLordy
04-07-2003, 11:57 AM
that is excellent!
Thank you all very much, you've made my life a lot easier.
Rob