1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リダイレクト演算子について深堀りしてみる

Posted at

大前提

  • >標準出力 (STDOUT) に対して作用するリダイレクト演算子
  • echoprintは、すべて STDOUT に出力される
  • fwrite() は出力先を指定できるので
    • fwrite(STDOUT, ...) とすれば標準出力
    • fwrite(STDERR, ...) とすれば標準エラー出力

で、ポイントはここ。

標準エラー出力にした場合、> だけではファイルに出力されない

  • これをログに残すには、2> または 2>&1のような表記が必要

確かに実際の体験として

Webサーバーで webhook を利用して、
GitHub で push されたら本番端末で git pull するよう実装し、
そのとき、ログを保存する処理を入れたが、失敗時のエラーが保存されなかった。

原因は、git pull のエラー出力が STDERRに出力されていたため、> ではキャッチされなかったためだった。

まとめ

  • > は標準出力(STDOUT)にしか動作しない
  • print echo = STDOUT
  • fwrite() は出力先を指定できる
  • fwrite(STDERR, ...)で出力されたメッセージをログに残すには 2>2>&1のような表記が必要
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?