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
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