Link to home
Start Free TrialLog in
Avatar of gram77
gram77Flag for India

asked on

script to search files containing searchword?

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
Avatar of lbertacco
lbertacco

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 ?
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
 
Avatar of Tintin
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




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.
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.
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.
Avatar of gram77

ASKER

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

SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gram77

ASKER

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.