Click to See Complete Forum and Search --> : Shell scripting questions


Gorkwobbler
08-24-2001, 06:18 PM
I am writing a shell script called .searchfor, that takes two arguments - a search string and a directory. It searches for filenames that are the same as the search string in that directory and in all subdirectories. It works under the right conditions, but it has 2 problems.

First off, I want it to search for filenames CONTAINING the search string, so I don't have to enter the whole filename. How do I do this correctly with the wildcards?

Second, whenever it tries to search a directory that is protected by Linux permissions, it goes into an infinite loop. This is because it doesn't detect the error in the directory change and tries to go ahead with the for loop using an invalid for loop variable. The cd command doesn't seem to return an error code that can be checked; how do I get the program to check for an error before executing the loop?

I reproduced the code of the script below. (Netscape wouldn't let me paste from the emacs 'kill' buffer) =(. Any useful guidance would be much appreciated.

P.S. I could use GNOME's search feature, but I don't trust the X GUI's much, and it's more fun to write my own. Well, except for the fish named Wanda - I trust the fish.


#!/bin/bash

cd #2;
echo -n "Searching in: ";
echo `pwd`;

currentlist=`ls`; #holds listing of current directory;

for filename in $currentlist; do
{
if [ $filename = $1 ]
then
{
echo $filename;
echo -n " in: ";
echo `pwd`;
}
fi

if [ -d $filename ]
then
{
.searchfor $1 $filename;
}
fi
}
done

takshaka
08-24-2001, 11:32 PM
Okay. I'll be the first arse to say "what's wrong with the 'find' command?" :confused:

bwkaz
08-26-2001, 07:03 PM
Well, after I got rid of some things that I'm not entirely sure were correct, it works when I just put asterisks on both sides of the $1 argument to if:

if [ $filename = *$1* ]

Should that #2 at the start be a $2?

Should there be semicolons at the end of most of those lines (I don't think so, but maybe it doesn't matter.....)?

Anyway, yeah, after I added the asterisks, changed #2 to $2, and got rid of the semicolons, it worked fine here.

Not sure about the permissions thing though.

<edit> Oh yeah, I forgot: I changed the .searchfor line to ./searchfor also </edit>

[ 26 August 2001: Message edited by: bwkaz ]

The Kooman
08-26-2001, 11:41 PM
Originally posted by takshaka:
<STRONG>Okay. I'll be the first arse to say "what's wrong with the 'find' command?" :confused:</STRONG>

I agree - find would be the neatest solution:

find /start/dir -name '*search_string*'

Gorkwobbler
08-30-2001, 02:57 PM
Ok, I got the permissions thing to work by adding -r and -L checks to the second if statement, so that line now reads like this:
if [ -d $filename ] && [ -r $filename ] && ! [ -L $filename ]
(The -L check prevents another infinite recursion problem, where it tries to search a symbolic link to a directory that has already been searched.)

However when I add asterisks to either side of the $1, as suggested, it says ".searchfor: [: too many arguments". Why & how do I fix it?

P.S. Sorry you were correct the #2 should have been a $2. I retyped it incorrectly from emacs (netscape won't paste from the emacs clipboard).

bwkaz
08-30-2001, 08:15 PM
Ummm.... No ideas, sorry. :(

Maybe the find command would work better, as others have suggested?

The Kooman
08-31-2001, 01:03 AM
Originally posted by Gorkwobbler:
<STRONG>Ok, I got the permissions thing to work by adding -r and -L checks to the second if statement, so that line now reads like this:
if [ -d $filename ] && [ -r $filename ] && ! [ -L $filename ]
(The -L check prevents another infinite recursion problem, where it tries to search a symbolic link to a directory that has already been searched.)

However when I add asterisks to either side of the $1, as suggested, it says ".searchfor: [: too many arguments". Why & how do I fix it?

P.S. Sorry you were correct the #2 should have been a $2. I retyped it incorrectly from emacs (netscape won't paste from the emacs clipboard).</STRONG>

Try enclosing the "$filename" in DOUBLE quotes. Single quotes won't work.

Another thing to do is run the command as
"bash -x searchfor"

That'll print gobs of debugging info so you'll get an idea as to whats going on and whats going wrong.

HTH