[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: recursive find in current and parent etc until an item is found
From: |
Bernhard Voelker |
Subject: |
Re: recursive find in current and parent etc until an item is found |
Date: |
Sat, 11 Apr 2020 11:17:46 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 |
On 2020-04-10 19:29, Peng Yu wrote:
> On 4/10/20, Bernhard Voelker <address@hidden> wrote:
>> ---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---
>> #!/bin/sh
>>
>> f="$1" \
>> && test -n "$f" \
>> || { echo "Usage: $0 FILE" >&2; exit 1; }
>>
>> p="."
>>
>> # Search until the parent is identical to the current directory (='/').
>> until [ "$p" -ef "$p/.." ]; do
>> if [ -e "$p/$f" ]; then
>> echo "$p/$f"
>> exit 0
>> fi
>> p="$p/.."
>> done
>>
>> # Now we're in the '/' dir; check once again.
>> if [ -e "$p/$f" ]; then
>> echo "$p/$f"
>> exit 0
>> fi
>>
>> echo "'$f' not found" >&2
>> exit 1
>> --->8--->8--->8--->8--->8--->8--->8--->8--->8--->8--->8---
>>
>> Have a nice day,
>> Berny
>>
> Thanks. I will need to search the parent directory recursively. Does
> this code only search whether a file is in a parent their parents,
> etc., but not recursively for a given ancestor?
it looks in the current directory, then its parent, then its grandparent,
... until either the file has been found, or until a directory is identical
with its own parent (which is only true for the '/' root directory.
Have a nice day,
Berny