再帰的なbash関数(ディレクトリイテレータ)

再帰的なbash関数(ディレクトリイテレータ)

次の作業は、より簡単な「検索」アノテーションを使用して実行できることを認識していますが、再帰呼び出しを使用してソリューションを実装しようとしています。特定のディレクトリを調べて、すべてのサブディレクトリにあるファイル名の最大長を取得しようとしています。しかし、私の再帰は1レベル以下でのみ機能するため、デフォルトでは特定のディレクトリまたはそのサブディレクトリで最も長いファイル名を返します。

#! /bin/bash

export maxlen=0

findmaxr()
{
        if [ $# -eq  0 ] ; then
        echo "Please pass arguments. Usage: findmax dir"
        exit -1
        fi

        if [ ! -d "$1" ];       then
        echo "No such directory exist."
        exit -2
        fi

        for file in $(/bin/ls $1)
                do
                if [ -d "$file" ] ; then
                        findmaxr $file   # Recursively call the method for subdirectories
                else
                        cur=${#file}
                        if [ $maxlen -lt $cur ] ; then
                                maxlen=$cur
                        fi
                fi
                done

        echo "The file with the longest name has [$maxlen] characters."



}

findmaxr `pwd`

ベストアンサー1

努力する:

#! /bin/bash
export maxlen=0

findmaxr()
{
        if [ $# -eq  0 ] ; then
        echo "Please pass arguments. Usage: findmax dir"
        exit 1
        fi

        if [ ! -d "$1" ];       then
        echo "No such directory exist."
        exit 2
        fi

        for file in "$1"/*
                do
                if [ -d "$file" ] ; then
                        findmaxr "$file"   # Recursively call the method for subdirectories
                else
                        f=${file##*/}
                        cur=${#f}
                        if [ $maxlen -lt $cur ] ; then
                                maxlen=$cur
                        fi
                fi
                done
}

findmaxr "$PWD"
echo "The file with the longest name has [$maxlen] characters."

議論する

次のことをしないでください。

for file in $(/bin/ls $1)

ファイル名にスペースが含まれていない場合でも、解析はls依然として信頼できません。

これと比較して、これは安定して動作します。

for file in "$1"/*

このフォームには、サブディレクトリにドロップダウンするために必要な親ディレクトリの名前も保持されます。たとえば、次の例のディレクトリ構造を使用すると、出力にはlsそのディレクトリのデフォルトのファイル名のみが表示されますd 1

$ ls 'd 1'
12345  d 2

これと'd 1'/*は対照的に、出力には親ディレクトリの名前が含まれます。

$ echo 'd 1'/*
d 1/12345 d 1/d 2

親ディレクトリ名を保持することで、再帰操作を実行できます。

file完全なパスが含まれているので、長さを決定する前にディレクトリ名を削除する必要があります。これはプレフィックスを削除することによって行われます。

f=${file##*/}

はい

次のファイル構造を考えます。

$ find .
.
./d 1
./d 1/12345
./d 1/d 2
./d 1/d 2/1234567
./d 1/d 2/d 3
./d 1/d 2/d 3/123456789
./script

それではスクリプトを実行してみましょう。

$ bash script 
The file with the longest name has [9] characters.

おすすめ記事