このリダイレクトは有効ですか? 「2<&1」

このリダイレクトは有効ですか? 「2<&1」

以前に作業していたサーバーでコメントを見つけましたが、リダイレクトが正しくないようです。誰かが確認できますか?

これは、このHP-UXサーバーで実行されているアプリケーション/データベースを維持するサードパーティベンダーによって作成されたcrontabのエントリです。

15 21 * * 0-6 /usr/sys/bin/stop  q  >/stopout   2<&1 #
30 21 * * 0-6 /usr/sys/force     q  >/out       2<&1 #
55 23 * * 0-6 /usr/sys/start     q  >/startout  2<&1 #

Bashのマニュアルでこれを見つけましたが、これはstderrとstdoutが> / {stopout、out、startout}に行き、stderrがstdoutをコピーすることを意味します(stdoutがstopout、out、startoutに2回書き込まれることを意味しますか?)?混乱しています:)

3.6.8 Duplicating File Descriptors
The redirection operator

[n]<&word
is used to duplicate input file descriptors. If word expands to one or more 
digits, the file descriptor denoted by n is made to be a copy of that file 
descriptor. If the digits in word do not specify a file descriptor open for 
input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n 
is closed. If n is not specified, the standard input (file descriptor 0) is 
used.

ベストアンサー1

これはオタイルの可能性が高いです2>&1>foo 2>&1cronjobの出力とエラーを保存する簡単な方法は、crontabでよく見られます(参照)。だから大学と大学アフリカ連合,...).


気づく:

  • ここでfd 1は書き込み用に開かれました(>/stopoutと同じ1>/stopout)。
  • これを行うかfd 2を2>&1実行し2<&1ても、次のようになります。dup2'd(少なくともLinuxでは、たぶん他のプラットフォームでも同様)をfd 1として指定するので、fd 1と同じ方法で使用できます。
    • これは、fd 1に書き込まれたデータがfd 2に入力として送信されるという意味ではなく、fd 1と2が同じであることを意味します。
  • したがって、fd 2への書き込みは失敗せず、/stopoutfd 1に書き込むデータのように送信されます。

ただし、fd 1 が最初に読み取るために開かれると、どちらか一方への書き込みは失敗します。

$ strace -e write bash -c 'echo bar 1<foo 2<&1'
write(1, "bar\n", 4)                    = -1 EBADF (Bad file descriptor)
write(2, "bash: line 0: echo: write error:"..., 53) = -1 EBADF (Bad file descriptor)
+++ exited with 1 +++

おすすめ記事