Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1438661
  • 博文数量: 244
  • 博客积分: 3353
  • 博客等级: 中校
  • 技术积分: 3270
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 17:56
文章分类

全部博文(244)

文章存档

2023年(7)

2022年(7)

2021年(4)

2020年(1)

2019年(2)

2017年(2)

2016年(3)

2015年(11)

2014年(20)

2013年(10)

2012年(176)

分类: LINUX

2014-09-12 10:35:15

linux shell下常用输入输出操作符是:

1.  标准输入   (stdin) :代码为 0 ,使用 < 或 << ; /dev/stdin -> /proc/self/fd/0   0代表:/dev/stdin
2.  标准输出   (stdout):代码为 1 ,使用 > 或 >> ; /dev/stdout -> /proc/self/fd/1  1代表:/dev/stdout
3.  标准错误输出(stderr):代码为 2 ,使用 2> 或 2>> ; /dev/stderr -> /proc/self/fd/2 2代表:/dev/stderr


  1. [chengmo@centos5 shell]$ exec 6>&1
  2. #将标准输出与fd 6绑定
  3.  
  4. [chengmo@centos5 shell]$ ls /proc/self/fd/
  5. 0 1 2 3 6
  6. #出现文件描述符6
  7.  
  8. [chengmo@centos5 shell]$ exec 1>suc.txt
  9. #将接下来所有命令标准输出,绑定到suc.txt文件(输出到该文件)
  10.  
  11. [chengmo@centos5 shell]$ ls -al
  12. #执行命令,发现什么都不返回了,因为标准输出已经输出到suc.txt文件了
  13.  
  14. [chengmo@centos5 shell]$ exec 1>&6
  15. #恢复标准输出
  16.  
  17.  
  18. [chengmo@centos5 shell]$ exec 6>&-
  19. #关闭fd 6描述符
  20.  
  21. [chengmo@centos5 ~]$ ls /proc/self/fd/
  22. 0 1 2 3

  23. http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html


复杂一点实例

  1. exec 3<>test.sh;
  2. #打开test.sh可读写操作,与文件描述符3绑定
  3.  
  4. while read line<&3
  5.  do
  6.     echo $line;
  7. done
  8. #循环读取文件描述符3(读取的是test.sh内容)
  9. exec 3>&-
  10. exec 3<&-
  11. #关闭文件的,输入,输出绑定

阅读(2089) | 评论(0) | 转发(0) |
0

上一篇:怀念过去

下一篇:英文缩写

给主人留下些什么吧!~~