免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2516 | 回复: 5
打印 上一主题 下一主题

[文件目录] 重定向的顺序不一样会有啥影响? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-12-04 16:02 |只看该作者 |倒序浏览
目录下只有test.sh这个文件
$ ls test.sh testsh.sh
ls: 无法访问test.sh: 没有那个文件或目录
testsh.sh

然后重定向一下:
$ ls test.sh testsh.sh 2>output.txt 1>&2
$ cat output.tx
ls: 无法访问test.sh: 没有那个文件或目录
testsh.sh

换个顺序重新重定向
$ls test.sh testsh.sh 1>&2 2>output.txt
testsh.sh
$ cat output.txt
ls: 无法访问test.sh: 没有那个文件或目录

为什么2>output.txt和1>&2改变个顺序结果就不一样了呢?

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2013-12-04 16:21 |只看该作者
本帖最后由 jason680 于 2013-12-05 11:28 编辑

回复 1# niannian

in my understood

cmd ... 2>output.txt 1>&2
  step 1: 2>output.txt
    before: 1==> stdout,     2==> stderr
    after : 1==> stdout,     2==> output.txt
  step 2: 1>&2
    before: 1==> stdout,     2==> output.txt
    after : 1==> output.txt, 2==> output.txt

Note: you CANNOT use this way, cmd ... 2>output.txt 1>output.txt, in command line

cmd ... 1>&2 2>output.txt
  step 1: 1>&2
    before: 1==> stdout,     2==> stderr
    after : 1==> stderr,     2==> stderr
  step 1: 2>output.txt
    before: 1==> stderr,     2==> stderr
    after : 1==> stderr,     2==> output.txt
   

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
3 [报告]
发表于 2013-12-04 16:34 |只看该作者

  1. Note that the order of redirections is significant.  For example, the command

  2.               ls > dirlist 2>&1

  3.        directs both standard output and standard error to the file dirlist, while the command

  4.               ls 2>&1 > dirlist

  5.        directs  only  the standard output to file dirlist, because the standard error was duplicated as standard
  6.        output before the standard output was redirected to dirlist.

  7.    Redirecting Standard Output and Standard Error
  8.        Bash allows both the standard output (file descriptor 1) and the standard error output  (file  descriptor
  9.        2) to be redirected to the file whose name is the expansion of word with this construct.

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

  11.               &>word
  12.        and
  13.               >&word

  14.        Of the two forms, the first is preferred.  This is semantically equivalent to

  15.               >word 2>&1
复制代码
默认情况下:

标准输入(stdin)     --> 文件描述符 0 ==》 键盘
标准输出(stdout)   --> 文件描述符 1 ==》 屏幕
标准错误(stderr) --> 文件描述符2 ==》 屏幕

改变了默认的输出指向。

$ ls test.sh testsh.sh 2>output.txt 1>&2

2>output.txt  //本来应该输出屏幕上的东西现在输出了文件中去了,2 ==》文件中
1>&2 //本来输出到屏幕的指向了2,但是此时 2已经指向了文件 ,所以 1 ==》 文件中
所以标准输出和标准错误都在文件中了

$ ls test.sh testsh.sh 1>&2 2>output.txt

1>&2 // 同上,标准输出重定向到标准错误,即 输出屏幕的标准输出 还是 输出到屏幕
2>output.txt // 标准错误重定向到文件中,即本来输出到屏幕的东西现在却输出到了文件中了

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:58:11
4 [报告]
发表于 2013-12-04 16:37 |只看该作者
回复 3# reyleon
马克

   

论坛徽章:
0
5 [报告]
发表于 2013-12-05 11:22 |只看该作者
回复 2# jason680

我靠。。。您这个解释的真好啊。。。一阵检血啊。。


   

论坛徽章:
0
6 [报告]
发表于 2013-12-05 11:22 |只看该作者
回复 3# reyleon


    这个也谢谢。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP