出力リダイレクト

linux特有ですよね


[考え方]
標準出力って考え方が存在します
たとえばlsコマンドを普通に実行すると

# ls -l
drwxr-xr-x 2 root root 4096 1月 11 08:53 Desktop
rw------- 1 root root 1199 11月 21 19:19 anaconda-ks.cfg
rw-r--r-- 1 root root 37121 11月 21 19:18 install.log
rw-r--r-- 1 root root 3865 11月 21 19:06 install.log.syslog

と画面に出力されます
そう、デフォルトの標準出力は「画面」なのです
これをファイルに出力したり、プリンタに出力したりすることも(当然ですが)出来ます
これを出力リダイレクトというらしいです

同じコマンドで出力先をファイルにしてみます
# ls -l > testfile
# cat testfile
drwxr-xr-x 2 root root 4096 1月 11 08:53 Desktop
rw------- 1 root root 1199 11月 21 19:19 anaconda-ks.cfg
rw-r--r-- 1 root root 37121 11月 21 19:18 install.log
rw-r--r-- 1 root root 3865 11月 21 19:06 install.log.syslog


[記号]
記号使い方出力先
 >>[ファイル名]結果をファイルに出力
>>>>[ファイル名]結果をファイルに追記
2>2>[ファイル名]発生したエラーをファイルに出力
2>>2>>[ファイル名]発生したエラーをファイルに追記
 > 2>&1>[ファイル名]2>&1結果とエラーをファイルファイルに出力
>> 2>>&1>[ファイル名]2>&1結果とエラーをファイルに追記


[使用例]
# ls damiefile
ls: damiefile: そのようなファイルやディレクトリはありません  damiefileというファイルはない
# ls damiefile 2> err-txt  エラーが出たときはerr-txtにその内容を出力する
# cat err-txt
ls: damiefile: そのようなファイルやディレクトリはありません  エラーがerr-txtに出力されている
タグ

このページへのコメント

d9y1tJ Thanks again for the blog post.Much thanks again. Keep writing.

0
Posted by watch for this 2013年12月20日(金) 04:30:36 返信

Windowsでも同じようにできます。
Linux特有というわけではないです。

0
Posted by 通りすがり 2009年10月08日(木) 05:44:26 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

カテゴリ

考え中

とりあえずブックマークに飛んでね
bookmark

管理人/副管理人のみ編集できます