Web Server forum
Back To The Forum Home!Search!Private Messaging System

This is Interesting: Free IT Magazines Now Free shipping to   
Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Setting echoed string to command




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Setting echoed string to command  
Garbunkel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-04 10: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





[ Post a follow-up to this message ]



    Re: Setting echoed string to command  
Icarus Sparry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-04 07: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/n ull 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.




[ Post a follow-up to this message ]



    Re: Setting echoed string to command  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-04 07: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] > j=`expr $j + 1` > done
-- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:08 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 

Back To The Top
Home | Usercp | Faq | Register