LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 458|回复: 1

怎样重定向指令执行过程中关于执行情况的信息

[复制链接]
发表于 2005-5-16 09:03:56 | 显示全部楼层 |阅读模式
例如指令执行中,系统打印出来的错误信息,警告信息,进度信息(不是执行结果),等等,怎么样重定向到某个文件呢
发表于 2005-5-16 09:26:49 | 显示全部楼层
Quoted from Bash-Prog-Intro-Howto:

All about redirection

  3.1.

  Theory and quick reference

  There are 3 file descriptors, stdin, stdout and stderr (std=standard).



  Basically you can:

  1. redirect stdout to a file

  2. redirect stderr to a file

  3. redirect stdout to a stderr

  4. redirect stderr to a stdout

  5. redirect stderr and stdout to a file

  6. redirect stderr and stdout to stdout

  7. redirect stderr and stdout to stderr

     1 'represents' stdout and 2 stderr.

  A little note for seeing this things: with the less command you can
  view both stdout (which will remain on the buffer) and the stderr that
  will be printed on the screen, but erased as you try to 'browse' the
  buffer.

  3.2.  Sample: stdout 2 file

  This will cause the ouput of a program to be written to a file.


               ls -l > ls-l.txt



  Here, a file called 'ls-l.txt' will be created and it will contain
  what you would see on the screen if you type the command 'ls -l' and
  execute it.

  3.3.  Sample: stderr 2 file

  This will cause the stderr ouput of a program to be written to a file.


               grep da * 2> grep-errors.txt



  Here, a file called 'grep-errors.txt' will be created and it will con­
  tain what you would see the stderr portion of the output of the 'grep
  da *' command.
3.4.

  Sample: stdout 2 stderr

  This will cause the stderr ouput of a program to be written to the
  same filedescriptor than stdout.


               grep da * 1>&2



  Here, the stdout portion of the command is sent to stderr, you may
  notice that in differen ways.

  3.5.  Sample: stderr 2 stdout

  This will cause the stderr ouput of a program to be written to the
  same filedescriptor than stdout.


               grep * 2>&1



  Here, the stderr portion of the command is sent to stdout, if you pipe
  to less, you'll see that lines that normally 'dissapear' (as they are
  written to stderr) are being kept now (because they're on stdout).

  3.6.  Sample: stderr and stdout 2 file

  This will place every output of a program to a file. This is suitable
  sometimes for cron entries, if you want a command to pass in absolute
  silence.


               rm -f $(find / -name core) &> /dev/null



  This (thinking on the cron entry) will delete every file called 'core'
  in any directory. Notice that you should be pretty sure of what a com­
  mand is doing if you are going to wipe it's output.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表