LinuxSir.cn,穿越时空的Linuxsir!

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

请教关于重定问的&符号问题

[复制链接]
发表于 2007-4-5 17:10:20 | 显示全部楼层 |阅读模式
cmd > file 2>&1 把 stdout 和 stderr 一起重定向到 file 文件中
在这个命令中,&是什么意思?
2>1不行吗?这是不是表把错误重定向到输出中,也就是把错误和输出一起,对不?
发表于 2007-4-5 17:48:06 | 显示全部楼层
2>1 就是把 stderr 定向到 文件 1,,不是 stdout
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-5 21:11:54 | 显示全部楼层
那我们常听到说linux有0-9十个FD,这种FD是什么意思?
让我不解的是这个&号
回复 支持 反对

使用道具 举报

发表于 2007-4-5 23:17:45 | 显示全部楼层
file descriptor 吧。。。不清楚具体意思:)。

& 就是用来指定 FD 的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-6 08:53:25 | 显示全部楼层
&号,就我知道的就是表示后台运行。在这里如果说是指定FD的话,也说得过去。
那2 >&1 就是FD2=FD1 。如果没有&这个号,就表示>到1这个文件,如果有这个号就表示的是FD几了。对不?
有什么原始的资料可以证明这点?
回复 支持 反对

使用道具 举报

发表于 2007-4-6 09:42:35 | 显示全部楼层
建议你看看置顶的帖子。。。。或者你去读读 关于bash 的资料
回复 支持 反对

使用道具 举报

发表于 2007-4-6 09:44:32 | 显示全部楼层
Post by ipv6ok

有什么原始的资料可以证明这点?

man bash
/REDIRECTION
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-7 08:22:22 | 显示全部楼层
谢谢楼上的。呵。英文不是很好。真是笨啊。用man都不知道。

  1. [root@Server-tec root]# ls 2 2>&1 mm
  2. ls: 2: No such file or directory
  3. ls: mm: No such file or directory
  4. [root@Server-tec root]# cat mm
  5. cat: mm: No such file or directory
  6. [root@Server-tec root]# ls 2 > mm  2>&1
  7. [root@Server-tec root]# cat mm
  8. ls: 2: No such file or directory
  9. [root@Server-tec root]# ls 2 > mm  2>mm2
  10. [root@Server-tec root]# cat mm
  11. [root@Server-tec root]# cat mm2
  12. ls: 2: No such file or directory
  13. [root@Server-tec root]# ls 2 >&mm
  14. [root@Server-tec root]# cat mm
  15. ls: 2: No such file or directory
  16. [root@Server-tec root]# ls 2 &>mm
  17. [root@Server-tec root]# cat mm
  18. ls: 2: No such file or directory
  19. [root@Server-tec root]#

复制代码

原文帮助
Redirecting Standard Output and Standard Error
       Bash  allows both the standard output (file descriptor 1) and the stan-
       dard error output (file descriptor 2) to  be  redirected  to  the  file
       whose name is the expansion of word with this construct.

       There  are  two  formats  for  redirecting standard output and standard
       error:

              &>word
       and
              >&word

       Of the two forms, the first is preferred.  This is semantically equiva-
       lent to

              >word 2>&1

按原文的意思,想把standerr 和standout输出到同一个文件。有三个方法。
我一个个测试了一下,果然如此。
回复 支持 反对

使用道具 举报

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

本版积分规则

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