foreach shell scripting help.

Hello all,

I am fairly new to the freebsd environment in terms of shell scripting. Most of my experience is with hosting and server solutions on freebsd.

I am currently trying to write a shell script that will do the following.

1. find all index.php and index.html files. and place those values into a variable called $found_files.

2. cat those files, and search for the keyword "iframe"

3. Only print those results that it finds the keywords to the screen with the line number and path to the file it finds.


The issue I am having is getting the output to print the directory location of the file it finds.

Here is my script.

Code:
#!/bin/tcsh

foreach found_file ( `find . -type f \( -name "index.html" -o -name "index.php" \)`)
echo $found_file | xargs cat | grep -nH '<iframe'
end


If any one can shed some light on this I would be very appreciative.
 
I am always open to other options. I just chose tcsh because it's what I am most familiar with.

Do you know of a way to accomplish what I need in /bin/sh ??
 
Here's a quick thing I put together:
Code:
#!/bin/sh

#
# Basic audit
#
if [ ! -d "${1}" ] ; then
  echo "Usage: ${0} search_path"
  exit 1
fi

#
# Main logic
#
find ${1} \( -iname 'index.php' -or -iname 'index.html' \) \
  -exec grep -nH 'iframe' {} \;

exit 0

Output looks like:
Code:
> ./foo.sh bardir/
bardir/index.html:7:iframe
bardir/index.php:3:iframe
 
Back
Top