1

Can someone please explain how these process substitutions are working.

(echo "YES")> >(read str; echo "1:${str}:first";)> >(read sstr; echo "2:$sstr:two")> >(read ssstr; echo "3:$ssstr:three")

Output

1:2:3:YES:three:two:first

I've figured out, that the 'ssstr'-Substitution got FD 60, sstr FD 61 and str FD 62. (right to left)

But how is (echo "YES") connected to input of FD60, and output of FD60 with input FD61 and so on and finally FD62 prints out on Terminal ?

All against the direction of the two redirections.

How are they nested, and how connected ? Makes me crazy. Ty.

4

1 に答える 1

2

まず、実際にこのようなコードを書かないでください:)

プロセス置換は構成要素>(...)です。これ(...)>は特定の構造ではありません。これは単なるサブシェルであり、その後に出力リダイレクトが続きます。

(echo "YES")この例は、 3 つの出力リダイレクトが続く単一のコマンドです。

  1. > >(read str; echo "1:${str}:first";)
  2. > >(read sstr; echo "2:$sstr:two")
  3. > >(read ssstr; echo "3:$ssstr:three")

最後の 1 つは、元のコマンドに実際に適用されるものです。のようなものecho word >foo >bar >bazは 3 つのファイルすべてを作成しますが、の出力はechoにのみ書き込まれbazます。

同様に、3 つのプロセス置換はすべて新しいプロセスを開始しますが、出力は最後YESのプロセスにのみ書き込まれます。Soから入力を取得します。read ssstrecho YES

この時点で、未定義の動作に相当するものを見ていると思います。3 つのプロセス置換は、OS が次のプロセスが作成されるときに各プロセスをスタックにプッシュし、スタックからポップしてスケジュールするかのように、作成された逆の順序で実行されますが、その順序は正しくないと思います。何でも保証します。

ただし、いずれの場合も、各プロセス置換の標準入力は、コマンドの標準出力に固定されます。これは、実行された他のプロセス置換です。つまり、コマンドは最終的に次のようになります

echo YES | { 
  read ssstr
  echo "3:$ssstr:three" | {
    read sstr
    echo "2:$sstr:two" | {
      read str
      echo "1:$str:one"
    }
  }
}
于 2016-11-19T13:22:23.917 に答える