# foreach shell scripting help.



## cd3003 (Sep 4, 2009)

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.


```
#!/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.


----------



## anomie (Sep 4, 2009)

Is Bourne shell (/bin/sh) an option?


----------



## cd3003 (Sep 4, 2009)

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 (Sep 4, 2009)

Here's a quick thing I put together: 

```
#!/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: 

```
> ./foo.sh bardir/
bardir/index.html:7:iframe
bardir/index.php:3:iframe
```


----------



## cd3003 (Sep 4, 2009)

Nice, let me give this a try.


----------



## cd3003 (Sep 4, 2009)

It works great! thanks so much!


----------

