単純なシェル スクリプトでは、何千ものファイルを処理できません。正常に開始しますが、しばらくすると「一致する `"` を探しているときに予期しない EOF が発生しました」というメッセージが表示されます。

単純なシェル スクリプトでは、何千ものファイルを処理できません。正常に開始しますが、しばらくすると「一致する `"` を探しているときに予期しない EOF が発生しました」というメッセージが表示されます。

問題のシェルスクリプト

皆さんにもっと理解していただくために、私が何をしようとしているのかを例を挙げて説明しましょう。ディレクトリに 100 個の .torrent ファイルがあるとします。そのうちの 2 つを BitTorrent クライアントに追加すると、それぞれ xxx.epub と yyy.epub がダウンロードされますが、100 個のうちどの 2 つがダウンロードされるのかわかりません。

私のスクリプトは、(1) を使用してfindすべての .torrent ファイルを調べpwd、各 .torrent ファイルを渡すと、transmission-show.torrent ファイルが解析され、人間が読める形式でメタデータが出力されます。次に、 を使用してawktorrent ファイルがダウンロードするファイル名を取得し、探しているファイル名 (xxx.epub や yyy.epub など) を含む list.txt に対してそれを実行します。

ファイル: findtor-array.sh

#! /bin/bash
#
# Search .torrent file based on 'Name' field.
#
# USAGE:
# cd ~/myspace # location of .torrent files
# Run `findtor ~/list.txt` (if `findtor.sh` is placed in `~/bin` or `~/.local/bin`)

# Turn the list of file names from ~/list.txt (or any file passed as argument) into an array
readarray -t FILE_NAMES_TO_SEARCH < "$1"

# For each file name from the list...
for FILE_NAME in "${FILE_NAMES_TO_SEARCH[@]}"
do
    # In `pwd` and 1 directory-level under, look for .torrent files and search them for the file name
    find . -maxdepth 2 -name '*.torrent' -type f -exec bash -c "transmission-show \"\$1\" | awk '/^Name\: / || /^File\: /' | awk -F ': ' '\$2 ~ \"$FILE_NAME\" {getline; print}'" _ {} \; >> ~/torrents.txt

    # The `transmission-show` command included in `find`, on it own, for clarity:
    # transmission-show xxx.torrent | awk '/^Name: / || /^File: /' | awk -F ': ' '$2 ~ "SEARCH STRING" {getline; print}'
done

プロセスは単純で、正しく実行していると思います (ただし、チェックはありません)。しかし、どういうわけか、スクリプトを実行すると、しばらくすると、 + するまでこれらのエラーが継続的にスローされるため、タスク全体がスクリプトには多すぎるようCtrlですC

_: -c: line 0: unexpected EOF while looking for matching `"'
_: -c: line 1: syntax error: unexpected end of file

これらは「スケーリング」の問題ですか? 何が足りないのでしょうか? また、これを修正するにはどうすればいいのでしょうか?

答え1

FILE_NAMEコマンドのオプションbash -cに直接渡されます。引用符やシェルコードが含まれていると問題が発生します。実際、-execfindFILE_NAME任意のコードが実行される可能性がある例: この特定のケースでは、入力ファイルに次の行が含まれている可能性があります。'; echo "run commands";'

代わりに、ループ変数をbash -c位置パラメータとして渡します。例:

find . -maxdepth 2 -name '*.torrent' -type f -exec sh -c '
transmission-show "$2" |
awk -v search="$1" '\''/^Name: / {name = substr($0,7)} /^File: / && name ~ search {print; exit}'\' \
_ "$FILE_NAME" {} \;

また、各ファイルのすべての検索用語をループするのは非効率的です。次のようにファイルをループして検索することを検討してくださいgrep -f file

find . -maxdepth 2 -name '*.torrent' -type f -exec sh -c '
file=$1
shift
if transmission-show "$file" | head -n 1 | cut -d" " -f2- | grep -q "$@"; then
    printf "%s\n" "$file"
fi' _ {} "$@" \;

またはなしfind:

for file in *.torrent */*.torrent; do
    if transmission-show "$file" | head -n 1 | cut -d' ' -f2- | grep -q "$@"; then
        printf '%s\n' "$file"
    fi
done
  • 上記は、すべての引数を に渡すだけなので、使用法としては、固定文字列の場合は、 などのリストからパターンを取得することgrepになります。findtor -f ~/list.txt-F-e expression

答え2

@Kusalanandaからの提案、@guestと@Jetchiselによる回答、そしてケビンによる詳細な回答、私はこれを思いつきました:

#! /bin/bash
#
# Search for 'Name' field match in torrent metadata for all .torrent files in
# current directory and directories 1-level below.
#
# USAGE e.g.:
# cd ~/torrent-files # location of .torrent files
# Run `~/findtor.sh ~/list.txt`

# Get one file name at a time ($FILE_NAME_TO_SEARCH) to search for from list.txt
# provided as argument to this script.
while IFS= read -r FILE_NAME_TO_SEARCH; do

    # `find` .torrent files in current directory and directories 1-level under
    # it. `-print0` to print the full file name on the standard output, followed
    # by a null character (instead of the newline character that `-print` uses).
    #
    # While that's happening, we'll again use read, this time to pass one
    # .torrent file at a time (from output of `find`) to `transmission-show`
    # for the latter to output the metadata of the torrent file, followed by
    # `awk` commands to look for the file name match ($FILE_NAME_TO_SEARCH) from
    # list.txt.
    find . -maxdepth 2 -name '*.torrent' -type f -print0 |
        while IFS= read -r -d '' TORRENT_NAME; do
            transmission-show "$TORRENT_NAME" | awk '/^Name: / || /^File: /' | awk -F ': ' -v search_string="$FILE_NAME_TO_SEARCH" '$2 ~ search_string {getline; print}';
        done >> ~/torrents-found.txt

done < "$1"

これを実行したところ、今のところうまくいっているようです。関係者全員に心から感謝します。

最善を尽くしましたが、修正やさらなる提案があれば歓迎します。

答え3

私はこのように書きます。

#!/usr/bin/env bash

pattern_file="$1"

while IFS= read -r -d '' file; do
    transmission-show "$file" | awk .... "$pattern_file"   ##: Figure out how to do the awk with a file rather than looping through an array.
done < <(find . -maxdepth 2 -name '*.torrent' -type f -print0)

そうすれば、引用地獄は避けられるはずです :-)

まあ、おそらくはnullglob必要ないかもしれません。

編集:

find コマンドを試して、元のスクリプトで使用してください。

find . -maxdepth 2 -name '*.torrent' -type f -exec bash -c 'transmission-show "$1" | awk "/^Name\: / || /^File\: /" | awk -F ": " "\$2 ~ \"$FILE_NAME\" {getline; print}"' _ {} + >> ~/torrents.txt

関連情報