Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347254
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: LINUX

2016-07-12 22:51:57

linux文件描述符:
linux启动后,会默认打开3个文件描述符,分别是:标准输入standard input 0,正确输出standard output 1,错误输出:error output 2。
一个命令执行了:先有一个输入:输入可以从键盘,也可以从文件得到,命令执行完成:成功了,会把成功结果输出到屏幕:standard output默认是屏幕。命令执行有错误:会把错误也输出到屏幕上面:standard error默认也是指的屏幕。有时候并不希望执行结果输出到屏幕。我想输出到文件或其它设备。这个时候我们就需要进行输出重定向了。
输出重定向:
格式:
command-line1 [1-n] > file或文件操作符或设备
上面命令意思是:将一条命令执行结果(标准输出,或者错误输出,本来都要打印到屏幕上面的)  重定向其它输出设备(文件,打开文件操作符,或打印机等等)1,2分别是标准输出,错误输出。
举例:

点击(此处)折叠或打开

  1. [root@localhost ~]# ll file2 file1
  2. ls: 无法访问file1: 没有那个文件或目录
  3. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  4. #当前的目录下有file2文件,没有file1文件。
  5. #正确输出和错误输出都显示在屏幕上,把正确输出写入新文件success.txt;
  6. [root@localhost ~]# ll file2 file1 > success.txt
  7. ls: 无法访问file1: 没有那个文件或目录
  8. [root@localhost ~]# cat success.txt
  9. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  10. [root@localhost ~]# ll file2 file1 1> success1.txt
  11. ls: 无法访问file1: 没有那个文件或目录
  12. #此处错误输出到屏幕上
  13. [root@localhost ~]# cat success1.txt
  14. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  15. #1>可以省略不写,默认为标准输出
  16. #把错误输出,不输出到屏幕,输出到error.txt
  17. [root@localhost ~]# ll file2 file1 1>s1 2>error
  18. #此处没有错误输出到屏幕,把错误的输出重定向到了文件里,这样在屏幕上就不显示了;
  19. [root@localhost ~]# cat s1
  20. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  21. [root@localhost ~]# cat error
  22. ls: 无法访问file1: 没有那个文件或目录
  23. #继续追加正确输出到success.txt,错误输出到error.txt,使用追加操作符号
  24. [root@localhost ~]# ll file2 file1 1>>success.txt 2>>error
  25. [root@localhost ~]# cat success.txt error
  26. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  27. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  28. ls: 无法访问file1: 没有那个文件或目录
  29. ls: 无法访问file1: 没有那个文件或目录
  30. #通常打开的文件在进程推出的时候自动的关闭,但是更好的办法是当你使用完以后立即关闭。用m<&-来关闭输入文件描述符m,用m>&-来关闭输出文件描述符m。如果你需要关闭标准#输入用<&-; >&-被用来关闭标准输出。举例说明把错误输出信息关闭掉;
  31. [root@localhost ~]# ll file2 file 1>success.txt 2>&-
  32. [root@localhost ~]# ll file2 file 2>/dev/null
  33. -rw-r--r--. 1 root root 0 7月 12 21:23 file2
  34. #/dev/null 这个设备,是linux 中黑洞设备,什么信息只要输出给这个设备,都会给吃掉
  35. #关闭所有输出
  36. [root@localhost ~]# ll file2 file2 1>&- 2>&-
  37. #将错误输出2 绑定给 正确输出 1,然后将 正确输出 发送给 /dev/null设备
  38. [root@localhost ~]# ll file2 file >/dev/null 2>&1
  39. #& 代表标准输出,错误输出。将所有标准输出与错误输出输入到/dev/null文件
  40. [root@localhost ~]# ll file2 file &>/dev/null
  41. 输入重定向:
  42. 格式:
  43. command-line [n] <file或文件描述符&设备
  44. 命令默认从键盘获得的输入,改成从文件,或者其它打开文件以及设备输入。执行这个命令,将标准输入0,与文件或设备绑定。将由它进行输入。

  45. [root@localhost ~]# cat > test.txt
  46. this a
  47. test
  48. file[root@localhost ~]# cat test.txt
  49. this a
  50. test
  51. file
  52. #这里按下 [ctrl]+d 离开
  53. #从标准输入【键盘】获得数据,然后输出给test.txt文件
  54. [root@localhost ~]# cat > test.txt < test.sh
  55. -bash: test.sh: 没有那个文件或目录
  56. [root@localhost ~]# cat test.txt
  57. #test.txt文件已为空文件
  58. [root@localhost ~]# cat > test.txt << eof
  59. > this
  60. > is
  61. > a
  62. > test
  63. > file
  64. > eof
  65. #<< 这个连续两个小符号, 他代表的是『结束的输入字符』的意思。这样当空行输入eof字符,输入自动结束,不用ctrl+D


阅读(2155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~