LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script Problem (http://www.linuxquestions.org/questions/programming-9/shell-script-problem-754486/)

pstewart726 09-11-2009 04:24 PM

Shell Script Problem
 
Hi there... I'm not a good scripter (as will be obvious here lol)... trying to build a simple script that will:

Generate a directory list, search inside of each directory for a specific file (if it exists), and then search that file for matching criteria. In this particular example I'm searching for specific IP addresses throughout 300+ apache access logs.

Here's what I have created and I'm having a problem that I'm sure will be obvious..;)

#!/bin/sh
for dir in '/bin/ls /home/httpd/vhosts'
do
find /home/httpd/vhosts/$dir/statistics/logs/ -name access_log -exec \
egrep -il "193.169" {} \;
done


Output breaks with the following:

find: /home/httpd/vhosts//bin/ls: No such file or directory
find: /home/httpd/vhosts/statistics/logs/: No such file or directory

So it seems that the dir variable isn't passing two lines down or am I missing something here?

Any and all suggestions/help is most welcome.

Paul

TBC Cosmo 09-11-2009 04:31 PM

First thing I notice is that you are not using "grave marks" in
Code:

'/bin/ls /home/httpd/vhosts'
So you are literally passing /bin/ls and /home/httpd/vhosts to your find command.

Personally, I would try a recursive grep looking for that IP.

pstewart726 09-12-2009 07:32 AM

recursive grep
 
Thank you for the reply ... I"m searching and trying to find a way to have a recursive grep output the directory/filename where it finds a match?

This was what I started with originally was trying to recursively grep out a match from all files in a series of subdirectories but when it goes find a match there was no way (that I could find) to output the full path... I must be missing something? ;)

Paul

jlinkels 09-13-2009 08:31 AM

Depends a bit on how neat you want your output to be.

First off, change the first line in your script to:
Code:

for dir in $(/bin/ls /home/httpd/vhosts)
Most people overlook the backticks, $(...) is clearer.

Since you have $dir in the middle of you path, using a for loop is not such a bad idea.

Using grep -H makes grep print the file name it is processing including the directory.

jlinkels

d.h 09-16-2009 02:43 AM

Not sure if I understand your problem correctly, but this might be what you want:
Code:

#!/bin/sh
for DIR in '/bin/ls /home/httpd/vhosts'; do
  grep "193.169" /home/httpd/vhosts/$DIR/statistics/logs/*
done

This outputs the full path of all files in these directories that contain '193.169'.
The same as above can be achieved much simpler by:
Code:

grep "193.169" /home/httpd/vhosts/*/statistics/logs/*
You also can do something like this:
Code:

for FILE in /home/httpd/vhosts/*/statistics/logs/access_log; do
  if grep "193.169" $FILE >/dev/null; then
    echo "The file $FILE contains 193.169!"
  fi
done

or this:
Code:

for DIR in '/bin/ls /home/httpd/vhosts'; do
  for FILE in /home/httpd/vhosts/$DIR/statistics/logs/access_log; do
    if grep "193.169" $FILE >/dev/null; then
      echo "The file $FILE contains 193.169!"
    fi
  done
done

It all depends on what exactly you want to do. Just play arround with it and see what it does.

matonb 09-16-2009 04:19 AM

Quote:

Originally Posted by pstewart726 (Post 3679867)
Thank you for the reply ... I"m searching and trying to find a way to have a recursive grep output the directory/filename where it finds a match?

This was what I started with originally was trying to recursively grep out a match from all files in a series of subdirectories but when it goes find a match there was no way (that I could find) to output the full path... I must be missing something? ;)

Paul

Why not just use

Code:

grep -rl

matonb 09-17-2009 03:41 AM

Just to expand a bit:

Code:

[matonb@dev ~]$ grep -rl "193\.169" /tmp
/tmp/lq/test1

If you pass grep the base path to search it will be included in the output. You also need to escape the period '.' as in regular expressions that means any character.

Code:

[matonb@dev ~]$ grep -rl "193.169" /tmp
/tmp/lq/test2
/tmp/lq/test1

/tmp/lq/test1:
Code:

193.169
/tmp/lq/test2:
Code:

193x169


All times are GMT -5. The time now is 04:57 PM.