0

私は Bash Scripting を初めて使用し、サブディレクトリを検索するディレクトリを反復処理し、それらの内部に入り、前述のサブディレクトリ内にある可能性のあるファイル内の特定の文字列を検索するスクリプトを作成したいと考えました。

私が現在抱えている問題は、実際にはその2番目の部分にあります。現在のサブディレクトリ内のすべてのファイルに 1 つずつアクセスしようとしていますが、うまくいきません。ばかげた質問かもしれませんが、私はこれが初めてなので、問題の解決策を見つけることができません。コードは次のとおりです。

if [[ $1 != "--help" ]]; 
then
    echo "counting results for string: "$1>results.txt
    let total=0

    for dir in */
    do
        if [[ -d $dir ]]; 
        then
            echo "directory: "$dir>>../results.txt
            let dirtotal=0
            cd $dir

            for i in */
            do
                if [[ -f $i ]]; 
                then
                    #look in the file i and count the number of occurrences
                    let result=`grep -c $1 $i`
                    echo $i": "$result
                    let dirtotal=$dirtotal+$result
                fi
            done

            echo "directory total: "$dirtotal>>../results.txt
            let total=$total+$dirtotal

            cd ..
        fi

    done

    echo "total: "$total>>results.txt

    exit 0

else
    display_error
    exit 1
fi

問題は、2 番目の for...do...done ループを作成するときに発生します。

ありがとう。

4

2 に答える 2

0

変化する

for i in */

に:

for i in *

サブディレクトリに加えてファイルに一致するようにします。

また、変数にスペースやワイルドカード文字が含まれている場合は、すべての変数を引用符で囲む必要があります。

于 2013-10-02T17:41:14.250 に答える