View Full Version : Bash - grep with user specified pattern [help]

October 4th, 2009, 06:21 AM
Hey there. I'm having trouble getting this script to work right.
I want to input a pattern for grep, 'mtu' for instance, but having $zSearch instead of 'mtu' doesn't work.

Here is the script.


read -p "Enter the target directory to search:" zTarget
read -p "Enter the search option ['mtu']:" zSearch
read -p "Would you like to start the script [Y/N]:" zStart
if [ -e $zStart ] || [[ $zStart == [nN]* ]]; then
echo "Exit 0"
exit 0; fi
find $zTarget -type f -name '*.c' | while read zIn; do
zOut=`grep -n -H $zSearch $zIn` # This is the problem
echo "$zOut"; done

# zName=`grep -n -H $zSearch $zFirst` doesn't work?
Why doesn't the user variable work? Is there another way to input a pattern to grep?
I'm trying to search a directory full of source code for specific functions.
Any help would be appreciated!

October 4th, 2009, 07:28 AM
Why don't you just do:

find . -name "*.c" | xargs grep -n -H <string>

Aside from that, do you mean:

if [ -e $zStart ] && [[ $zStart == [nN]* ]]; then


October 4th, 2009, 03:21 PM
Well I will have to try that. Thanks

And for this.

if [ -e $zStart ] && [[ $zStart == [nN]* ]]; then

If zStart has a length of zero or the letter n or N, then exit 0.
The way you have it is if it's empty and the letter n or N?
Oh and it should be [ -z $zStart ] not -e. My b I messed that up.