
Bashプロセスの交換を研究中に、次のことがわかりました。
counter=0
while IFS= read -rN1 _; do
((counter++))
done < <(find /etc -printf ' ')
echo "$counter files"
私が正しく理解した場合、findコマンドの出力は "_"を置き換えます。
しかし:
- これはどのようなメカニズムですか?
- また:何をすべきですか
read -rN1
?
修正する:
追加の質問:プロセス置換はwhileループで「完了」を指します。これがどのように機能するか、つまりwhileループがその時点で置換を実行する理由です。私が読むことができる一般的な内容はありますか?
答え1
<(find /etc -printf ' ')
これを「プロセス置換」といいます。それは作り出す一つ' '
ファイルごとの文字(スペース)です。の出力はfind /etc -printf ' '
ファイル(またはファイルの内容)として提供されます。ファイル名はコマンドラインから拡張されます。これ追加 <
このファイルでstdinリダイレクトを実行します。
read -rN1 _
(リダイレクトされた)stdinから名前付き変数を_
一度に1文字ずつ読み、文字数を数えます(各文字はファイルを表します)。
read
次の主張は次のとおりですman bash
。
-r Backslash does not act as an escape character. The backslash is considered
to be part of the line. In particular, a backslash-newline pair may not be
used as a line continuation.
-N nchars
read returns after reading exactly nchars characters rather than waiting for
a complete line of input, unless EOF is encountered or read times out.
Delimiter characters encountered in the input are not treated specially and
do not cause read to return until nchars characters are read.