LinuxQuestions.org
Have you heard the LinuxQuestions.org Podcast?
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 09-11-2009, 04:24 PM   #1
pstewart726
LQ Newbie
 
Registered: Sep 2009
Posts: 2
Thanked: 0
Shell Script Problem


[Log in to get rid of this advertisement]
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
windows_vista pstewart726 is offline     Reply With Quote
Old 09-11-2009, 04:31 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: FC9/10, RHEL
Posts: 327
Thanked: 22
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.
windows_xp_2003 TBC Cosmo is offline     Reply With Quote
Old 09-12-2009, 07:32 AM   #3
pstewart726
LQ Newbie
 
Registered: Sep 2009
Posts: 2
Thanked: 0

Original Poster
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
linuxubuntu pstewart726 is offline     Reply With Quote
Old 09-13-2009, 08:31 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 2,301
Thanked: 89
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
linuxdebian jlinkels is offline     Reply With Quote
Old 09-16-2009, 02:43 AM   #5
d.h
LQ Newbie
 
Registered: Sep 2009
Posts: 3
Thanked: 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 02:58 AM..
unknown d.h is offline  
Tag This Post , ,
Reply With Quote
Old 09-16-2009, 04:19 AM   #6
matonb
LQ Newbie
 
Registered: Aug 2006
Posts: 5
Thanked: 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
windows_98_nt_2000 matonb is offline     Reply With Quote
Old 09-17-2009, 03:41 AM   #7
matonb
LQ Newbie
 
Registered: Aug 2006
Posts: 5
Thanked: 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
windows_98_nt_2000 matonb is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 06:32 PM
Shell script problem maringat Linux - Software 5 07-25-2005 06:12 PM
A problem about a shell script jpan Linux - General 2 01-19-2005 07:14 PM
Shell-Script Problem? x4v013 Slackware 7 09-17-2003 01:15 AM


All times are GMT -5. The time now is 05:50 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration