?
Solved

script to search files containing searchword?

Posted on 2007-07-24
10
Medium Priority
?
271 Views
Last Modified: 2013-11-13
a. I need to create a shell script that searches all files under directories a,b,c,d
containing the word 'ftp' or 'put'.
And then print the log of resulting filenames containing the word 'ftp' to a file.

b. The files identified above are passed parameters such as HOSTNAME,DIRECTORY etc. as seen in the underlying script.

The shell script should identify which shell scripts are calling these files.
For example the program identifies that abc.sh calls def.sh; rst.sh calls xyz.sh and so on

---------
def.sh
---------
PID=$$
HOSTNAME=$1
DIRECTORY=$2
FILENAME=$3
SUFFIX=$4
USERNAME=$5
PASSWORD=$6
 
if [ -s $INTFILE ] && [ -f $INTFILE ]
then

      ftp -n $HOSTNAME << EOF
      quote user $USERNAME
      quote pass $PASSWORD
      cd $DIRECTORY
      ascii
      put $INTFILE $EXTFILE
      quit
EOF
0
Comment
Question by:gram77
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 11

Expert Comment

by:lbertacco
ID: 19557009
for point a) you just need grep

grep -lr "ftp" a b c d

I don't understand what you mean on point b). Do you mean that if some.sh is in the list returned from point a), then the script should look for other files containing the string "some.sh" ? Where should it look ?
0
 
LVL 7

Expert Comment

by:ezaton
ID: 19559912
Is it something like:

DIR=/my/scripts
for i in `egrep -rl "ftp|put" $DIR`; do
  echo "The script $i has the word ftp or get"
  echo "These scripts call $i:"
  grep -rl $i $DIR
done
 
0
 
LVL 48

Expert Comment

by:Tintin
ID: 19560303
a)

#!/bin/sh
LOG=/tmp/ftp.log
rm -f $LOG

for file in `find a b c d -type f -exec egrep -l "ftp|put" {} \;`
do
   echo "$file" >>$LOG
done

b)

#!/bin/sh
LOG=/tmp/ftp.log
rm -f $LOG

for file in `find a b c d -type f -exec egrep -l "ftp|put" {} \;`
do
   echo "$file" >>$LOG
done

for file in `cat $LOG`
do
   echo "$file is called by:"
   find a b c d -type f -exec grep -l $file" {} \;
   echo "-----------------"
done




0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:ezaton
ID: 19560363
Tintin - why using the expensive and slow 'find' command when you have GNU grep which can do the job with the flag '-r'? The find is good only for legacy type Unix, with non-GNU utils.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 19560443
ezaton.

Why are you assuming the poster is on a server that has GNU grep?  I posted portable code that can be used on any Linux/Unix system.
0
 
LVL 7

Expert Comment

by:ezaton
ID: 19560504
And would be expensive on Linux or any system not running GNU grep.

I agree that your code is portable, and I agree with your solution, however, my assumption was that in the "open source programming" section, GNU grep is rather common.
0
 

Author Comment

by:gram77
ID: 19562822
a. Files that are identified by step a are those files that do ftp for example def.sh (see the code under)

b. I want a script that tells me that the file identified in step a i.e. def.sh is called by file identified in step b i.e. abc.sh. abc.sh file calls def.sh and abc.sh passes parameters to def.sh.

Hope that makes things clearer.

---------
abc.sh            (--file identified by step b. this file calls the file that does ftp)      
---------
....
def.sh 121,dummyhost,/etc,myfile,sh,user,pass
...
...


---------
def.sh            (--file identified by step a. this file does ftp)
---------
PID=$$
HOSTNAME=$1
DIRECTORY=$2
FILENAME=$3
SUFFIX=$4
USERNAME=$5
PASSWORD=$6
 
if [ -s $INTFILE ] && [ -f $INTFILE ]
then

      ftp -n $HOSTNAME << EOF
      quote user $USERNAME
      quote pass $PASSWORD
      cd $DIRECTORY
      ascii
      put $INTFILE $EXTFILE
      quit
EOF

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 400 total points
ID: 19563151
a.
  #reading your description verbatime (beside it seems not to be what you really want)
  grep -l put {a,b,c,d}/*
  grep -l ftp {a,b,c,d}/* > files-with-ftp.log
  # and guessing what you want
  egrep -l "ftp|put" {a,b,c,d}/* > files-with-ftp-or-put.log

b.
  # this description is still ambigious, please describe again
  # anyway, I try to give a lazy assumtion ...
  egrep -l 'def.sh ' `cat files-with-ftp-or-put.log`



BTW, I agree with Tintin that grep's -r option should not be used, it's a fundamental break with the core unix theme :-/
0
 
LVL 48

Accepted Solution

by:
Tintin earned 1600 total points
ID: 19563451
Slight correction to my solution

b)

#!/bin/sh
LOG=/tmp/ftp.log
rm -f $LOG

for file in `find a b c d -type f -exec egrep -l "ftp|put" {} \;`
do
   echo "$file" >>$LOG
done

for file in `cat $LOG`
do
   file=`basename $file`
   echo "$file is called by:"
   find a b c d -type f -exec grep -l $file" {} \;
   echo "-----------------"
done
0
 

Author Comment

by:gram77
ID: 19564721
tintin:

The first program worked flawlessly. Thanks.


In the second program:
#!/bin/sh
LOG=/tmp/ftp.log
for file in `cat $LOG`
do
   file=`basename $file`
   echo "$file is called by:"
   find $XXAP_TOP -type f -exec grep -l "$file" {} \;
   echo "-----------------"
done

 find $XXAP_TOP -type f -exec grep -l "$file" {} \; is returning
all files it finds on it's way.

I want the output to print only those files that contain the program to invoke ftp.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Starting up a Project
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question