foreach shell scripting help.

cd3003

New Member


Messages: 4

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.
 
OP
OP
C

cd3003

New Member


Messages: 4

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 ??
 

anomie

Aspiring Daemon

Reaction score: 120
Messages: 781

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
 
Top