免费注册 查看新帖 |

Chinaunix

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

linux脚本之>/dev/null 2>&1,以及2>1 VS 2>&1 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-13 09:08 |只看该作者 |倒序浏览

1. 标准输入stdin文件描述符为0,标准输出stdout文件描述符为1,标准错误stderr文件描述符为2
2. /dev/null 空设备,相当于垃圾桶
3. 重定向符号:>
3. 2>1 与 2>&1 的区别
   2>1, 把标准错误stderr重定向到文件1中
   2>&1,把标准错误stderr重定向到标准输出stdout
4. 举例:
   假设有脚本test.sh,内容如下,t是一个不存在的命令,执行脚本进行下面测试。
   # cat test.sh
     t
     date
   标准输出重定向到log,错误信息输出到终端上,如下:
   # ./test.sh > log
     ./test.sh: line 1: t: command not found
   # cat log
     Thu Oct 23 22:53:02 CST 2008
   
   删除log文件,重新执行,这次是把标准输出定向到log,错误信息定向到文件1
   # ./test.sh > log 2>1
   #
   # cat log
   Thu Oct 23 22:56:20 CST 2008
   # cat 1
   ./test.sh: line 1: t: command not found
   #
   把标准输出重定向到log文件,把标准错误重定向到标准输出
   # ./test.sh > log 2>&1
   #
   # cat log
   ./test.sh: line 1: t: command not found
   Thu Oct 23 22:58:54 CST 2008
   #
   把错误信息重定向到空设备
   # ./test.sh 2>/dev/null
   Thu Oct 23 23:01:07 CST 2008
   #
   
   把标准输出重定向到空设备
   # ./test.sh >/dev/null
     ./test.sh: line 1: t: command not found
   把标准输出和标准错误全重定向到空设备
   #./test.sh >/dev/null 2>&1
   #
传自http://blog.csdn.net/submarine/archive/2008/10/23/3132824.aspx
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104425/showart_2068920.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP