This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2004 > Setting echoed string to command





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Setting echoed string to command
Garbunkel

2004-01-28, 5:38 am

Okay. I've only been doing the script thing for a couple of weeks
now, so please go easy on me...

I'm attempting to execute a program which will input from the user a
directory path, file extension, and a series of variables (user is
prompted for variables until they input that they're done). The
command that I want executed (one time for each variable input is:
'find <pathname> <extension> -exec grep -i <variablename> {} \;

The reason that I don't just do a simple grep is that there are too
many files in the target directory with the same file extension (I get
an error message if I try a simple grep).

Any help with how to pull this off would be much apprec. This is what
I have so far...


echo 'What path/directory are you searching in?:'
read pathdir
echo 'What file extension are you searching in?eg, <\*.h> )'
eval read fileext
eval echo 'Path: $pathdir'

i=0
searchagain="y"
while [ "$searchagain" = "y" ]
do
echo 'Enter in the variable to search for:'

eval read var$i
i=`expr $i + 1`

echo 'Do you want to enter another variable? (Y/N):'
read searchagain

done
j=0
while [ "$j" -lt "$i" ]
do
# this echoes to the screen the command that I want executed
eval echo \"var$j = \$var$j\"
eval echo -n "find $pathdir"\ '\'
eval echo -n "$fileext -exec grep \$var$j\ {}"\ '\'

# This is a failed attempt at executing the same thing that was echoed
# find '$'+$pathdir $fileext -exec grep \$var$j\ {} \; >>
varsfound.txt

j=`expr $j + 1`
done
Icarus Sparry

2004-01-28, 2:35 pm

On Wed, 28 Jan 2004 10:36:40 -0800, Garbunkel wrote:
quote:

> Okay. I've only been doing the script thing for a couple of weeks
> now, so please go easy on me...
>
> I'm attempting to execute a program which will input from the user a
> directory path, file extension, and a series of variables (user is
> prompted for variables until they input that they're done). The
> command that I want executed (one time for each variable input is:
> 'find <pathname> <extension> -exec grep -i <variablename> {} \;
>
> The reason that I don't just do a simple grep is that there are too
> many files in the target directory with the same file extension (I get
> an error message if I try a simple grep).
>
> Any help with how to pull this off would be much apprec. This is what
> I have so far...


[snipped]

Difficult to know where to start with this one. One thing which makes a
big difference is if the things you are searching for are fixed strings
or regular expressons. If they are fixed strings then something like

------------------------------------
#!/bin/sh
echo "Enter path to search from"
read dir
echo "Enter strings to search for, one per line, type CTRL-D to end"
filename=/tmp/findpat$$
cat > $filename
echo "Enter extension of files to search, including '.'"
read ext
find ${dir-.} -type f -name "*$ext" -print | xargs fgrep -f $filename /dev/null
rm $filename
------------------------------------

would do it. Of course it could be that you are trying to make a tool that
finds all uses of a particular variable name in a set of files. If so there
are other tools you can look at, the GNU 'id' tools are one example.

If you need to have the regular expressions, then you can build up the
pattern to search for, and then apply it all at once. Some versions of
grep (in particular GNU grep) have a '-f' option so you can replace
the fgrep with grep in the above example.

It may be worth while building up the list of files to search once, e.g.

--------------------------------------------------
#!/bin/sh
echo "Enter path to search from"
read dir
echo "Enter extension of files to search, including '.'"
read ext
filename=/tmp/findfiles$$
find ${dir-.} -type f -name "*$ext" -print > $filename
echo "Enter strings to search for, one per line, type CTRL-D to end"
while read pattern
do
xargs grep -i "$pattern" /dev/null < $filename
done
rm $filename
--------------------------------------------------
This is particularly true if you have a deep tree to search, but you imply
you are only interested in a particular directory. If that is the case, then
the following will work for the current directory
--------------------------------------------------
#!/bin/sh
echo "Enter extension of files to search, including '.'"
read ext
echo "Enter strings to search for, one per line, type CTRL-D to end"
while read pattern
do
ls -f | grep "$ext\$" |xargs grep -i "$pattern" /dev/null
done
--------------------------------------------------

Sorry, all of the above are untested.

Barry Margolin

2004-01-28, 2:35 pm

In article <5f8f7d6d.0401281036.163b2264@posting.google.com>,
mckrs@hotmail.com (Garbunkel) wrote:
quote:

> Okay. I've only been doing the script thing for a couple of weeks
> now, so please go easy on me...
>
> I'm attempting to execute a program which will input from the user a
> directory path, file extension, and a series of variables (user is
> prompted for variables until they input that they're done). The
> command that I want executed (one time for each variable input is:
> 'find <pathname> <extension> -exec grep -i <variablename> {} \;



Are you sure that's the command you want to execute? I think
<extension> should be -name <extension>.
quote:

> The reason that I don't just do a simple grep is that there are too
> many files in the target directory with the same file extension (I get
> an error message if I try a simple grep).
>
> Any help with how to pull this off would be much apprec. This is what
> I have so far...
>
>
> echo 'What path/directory are you searching in?:'
> read pathdir
> echo 'What file extension are you searching in?eg, <\*.h> )'
> eval read fileext
> eval echo 'Path: $pathdir'



You don't need eval in the above two commands.
quote:

> i=0
> searchagain="y"
> while [ "$searchagain" = "y" ]
> do
> echo 'Enter in the variable to search for:'
>
> eval read var$i



Consider using an array instead of eval.
quote:

> i=`expr $i + 1`
>
> echo 'Do you want to enter another variable? (Y/N):'
> read searchagain
>
> done
> j=0
> while [ "$j" -lt "$i" ]
> do
> # this echoes to the screen the command that I want executed
> eval echo \"var$j = \$var$j\"
> eval echo -n "find $pathdir"\ '\'
> eval echo -n "$fileext -exec grep \$var$j\ {}"\ '\'
>
> # This is a failed attempt at executing the same thing that was echoed
> # find '$'+$pathdir $fileext -exec grep \$var$j\ {} \; >>
> varsfound.txt



eval find $pathdir -name "'$fileext'" -exec grep \$var$j {} \\\;
quote:

[QUOTE][color=darkred]
> j=`expr $j + 1`
> done



--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2010 webservertalk.com