LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-11-2009, 03:24 PM   #1
pstewart726
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 09-11-2009, 03:31 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 355

Rep: Reputation: 43
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.
 
Old 09-12-2009, 06:32 AM   #3
pstewart726
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 09-13-2009, 07:31 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,065

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
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
 
Old 09-16-2009, 01:43 AM   #5
d.h
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
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.

Last edited by d.h; 09-16-2009 at 01:58 AM.
 
Old 09-16-2009, 03:19 AM   #6
matonb
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by pstewart726 View Post
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
 
Old 09-17-2009, 02:41 AM   #7
matonb
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
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
 
  


Reply

Tags
bash, grep, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Shell script problem maringat Linux - Software 5 07-25-2005 05:12 PM
A problem about a shell script jpan Linux - General 2 01-19-2005 06:14 PM
Shell-Script Problem? x4v013 Slackware 7 09-17-2003 12:15 AM


All times are GMT -5. The time now is 11:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration