Write script find.sh that traverses directories and subdirectories.

limbaon9 Posted messages 46 Status Membre -  
Cobalt222S Posted messages 7 Status Membre -
Hello everyone, it's me again ^^.
Well, this time I'm reaching out because I'm working on a shell script that searches for occurrences of a file or directory from a given directory passed as an argument. This command should recursively traverse the subdirectories by calling the program itself to perform a complete search. It displays on the standard output the paths where the entry is found. The program returns 0 (exit) on success, and 1 otherwise: find.sh [type] rep name. Here is the expected result:
>find.sh d /tmp r2
/tmp/src/r2
/tmp/src2/sr2/r2

>find.sh . f1
./f1 ./src/f1
./tmp/f1
>echo $?
0
>find.sh . fileThatDoesNotExist
>echo $?
1
For now, here's what I've done and it doesn't work because it shows me all the directories and subdirectories of the entered parameter for find.sh:

for file in "$1"*/ ; do
echo "$file"
done


And for my find function, I have this code (which doesn’t display anything by the way):
find()
{
if [$# -lt 2]
then
echo "usage(): find [type] rep -name file"
exit 1
fi
for file in "$1"/* ; do
if [ -d "$file" ] ; then
$0 "$file"
fi
echo "$file"
done
}


And I need to do the same question but this time with a function. I'm reaching out to you because I find that you are the best and that almost all my questions have been resolved so far.

5 réponses

zipe31 Posted messages 34620 Registration date   Status Contributeur Last intervention   6 501
 
Hello,

I admit I didn't fully grasp the purpose of what you want to do ;-(

Not to mention a few minor errors or not-so-orthodox methods ;-\

First of all, naming a function after an existing command isn't great, especially since you gave it almost the same way of passing options, which is confusing when reading your code ;-\

if [$# -lt 2]
There should be spaces on both sides of the brackets
if [ $# -lt 2 ]
.

$0 "$file"
Calling the script itself is also not ideal, at least for the way you're using it ;-\

In doing what you want, wouldn't calling the
find
command (the real one, not your function) be sufficient on its own for what I understand of your need?

--
_______________________________ ☯ Zen my nuggets ☮ _____________________________
Do something for the environment, close your windows and adopt a penguin… ????
0
limbaon9 Posted messages 46 Status Membre
 
Thank you for the information about spaces in brackets. And yes, calling the find command might be enough, but as mentioned on the subject, it's a shell script and a function that searches for a file or directory from a given directory argument, all using find. And in the end, I should expect a result like:

>find.sh . f1
./f1./src/f1
./tmp/f1
>echo $?
0
>find.sh . fileThatDoesNotExist
>echo $?
1

Maybe I wasn't clear about the purpose of what this script should do :-(
0
zipe31 Posted messages 34620 Registration date   Status Contributeur Last intervention   6 501
 
And all that with a find
Are we indeed talking about the command
/usr/bin/find
here?

So your function must/can call the command
find
and what matters is the tests to be done beforehand regarding the number of parameters on one hand, and on the other hand to determine if it’s a file that needs to be searched for or a file in a specific directory. Am I right?
0
limbaon9 Posted messages 46 Status Membre > zipe31 Posted messages 34620 Registration date   Status Contributeur Last intervention  
 
Yes exactly, my function can call the find command and we just need to test the number of arguments and whether it's a file or a directory. And I'm sorry for responding so late ^^.
0
limbaon9 Posted messages 46 Status Membre
 
Knowing also that to call the script we execute the following command:
source $0 $type (file or directory) $directory $name.
0
Cobalt222S Posted messages 7 Status Membre
 
Hi,

This exercise is about recursion.
In my humble opinion, we shouldn't use find which is recursive by default (we can block it, but then what's the point?), nor should we use a function since recursion is the script that must call itself to perform the recursion.

Let's recap:
The script receives 2 (path filename) or 3 arguments (type (how many are predefined?) path dirname), otherwise we exit with an error.

In pseudocode:

If nbArg is less than 2
then exit with error
otherwise
if nbArg equals 2
then
path equals the first argument
filename equals the second argument

for fileOrDir in path
if fileOrDir is a file AND fileOrDir equals path/filename
then display fileOrDir
otherwise
if fileOrDir is a directory
then execute script with fileOrDir filename
endIf

otherwise (nbArg equals 3)
for dirname in path (in bash we can limit path to directories only)
if dirname equals path/dirname
then
display dirname
execute script type dirname filename
endIf
endIf

This is neither checked nor tested: it's been a long time since I did recursion. :(
0
loftyramitsu Posted messages 43 Status Membre 1
 
And a little grep that would take the results from find and filter the data?
0
Cobalt222S Posted messages 7 Status Membre
 
This exercise is about recursion.
The result matters less than the demonstration.
0