Chinaunix首页 | 论坛 | 博客
  • 博客访问: 389343
  • 博文数量: 40
  • 博客积分: 1004
  • 博客等级: 准尉
  • 技术积分: 469
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-01 17:02
文章分类

全部博文(40)

文章存档

2011年(40)

分类: LINUX

2011-10-05 12:02:55

 I/O重定向是Linux的重要内容。I/O重定向就是一个过程,在这个过程中捕捉一个文件、命令、程序或脚本,甚至代码块的输出,然后把捕捉到的输出作为输入发送到另一个文件、命令、程序或脚本。

                           基本I/O重定向符号和意义

序号 (举例)

符号        

意义

1

cmd1 md2    

管道符,将cmd1的标准输出作为cmd2的标准输入

2

> filename

将标准输出写到文件filename

3

< filename

将文件filename的内容读入到标准输出中去

4

>> filename

将标准输出写到文件filename中,若filename存在则把内容追加到filename那么后面,

5

>| filename

即使noclobber选项开启,仍然强制将标准输出写到filename中,即把filename内容覆盖掉

6

n>|filename

即使noclobber选项开启,仍然强制将FDn的输出写到filename中,即把filename内容覆盖掉

7

n> filename

FDn的输出写到filename文件中

8

n< filename

将文件filename中内容读入到FD n

9

n>> filename

FDn的输出文件写到filename中,,若filename存在则把内容追加到filename那么后面,

10

<< delimiter

此处文档(Here——document

注:FD为文件标识符(File Descriptor

  文件标识符是从0到9结束的整数,指明了与进程有关的特定数据流源。当Linux系统启动一个进程(该进程可能用于执行Shell命令)时,将自动为该进程打开三个文件:标准输入、标准输出和标准错误输出,分别有文件标识符0、1、2标识,如果进程要打开其他的输入和输出文件,则从3开始标识。另外3-9是保留的标识符,可以把这些标识符指定为标准输入、标准输出和标准错误输出的临时连接。通常这样可以解决好多复杂的重定向请求

举例1

  1. [root@localhost shell]# ls -al |wc -l
  2. 95

2、

  1. [root@localhost shell]# cat >newfile #按住Ctrl+D结束编辑
  2. hello this is cherishyou's blog
  3. you can find some my dialog
  4. see you [root@localhost shell]#
  5. [root@localhost shell]# cat newfile
  6. hello this is cherishyou's blog
  7. you can find some my dialog
  8. see you

3

  1. [root@localhost shell]# wc < newfile #将newfile中的统计输出
  2. 2 13 68

4、

  1. [root@localhost shell]# ls /home/cherish/shell/ifexample/ >>newfile #在newfile文件后面追加内容
  2. [root@localhost shell]# cat newfile
  3. hello this is cherishyou's blog
  4. you can find some my dialog
  5. see you if
  6. if2
  7. if_exam
  8. if_exam1
  9. if_file1
  10. ifcopy
  11. ifeldel
  12. ifelifeise
  13. ifelse
  14. ifelse1
  15. ifelse2
  16. ifelse3

5、

  1. [root@localhost shell]# set -o noclobber  #开启noclobber(不允许覆盖任何文件)
  2. [root@localhost shell]# ls /home/cherish/shell/ifexample/ > newfile  #出现错误
  3. -bash: newfile: cannot overwrite existing file
  4. [root@localhost shell]# ls /home/cherish/shell/ifexample/ >| newfile  #正常运行 !!注意区别
  5. [root@localhost shell]# cat newfile
  6. if
  7. if2
  8. if_exam
  9. if_exam1
  10. if_file1
  11. ifcopy
  12. ifeldel
  13. ifelifeise
  14. ifelse
  15. ifelse1
  16. ifelse2
  17. ifelse3

6、

  1. [root@localhost shell]# ls z*  #无法访问
  2. ls: cannot access z*: No such file or directory
  3. [root@localhost shell]# ls z* >filefile   #出现错误
  4. ls: cannot access z*: No such file or directory
  5. [root@localhost shell]# cat filefile
  6. [root@localhost shell]# ls z* 2> filefile  #保存错误信息
  7. [root@localhost shell]# cat filefile
  8. ls: cannot access z*: No such file or directory

7、

  1. [root@localhost shell]# cat hello.c 1> filefile  #显示标准输出结果
  2. [root@localhost shell]# cat filefile
  3. #include
  4. main()
  5. {
  6. printf("HELLP\n");
  7. }

8、

  1. [root@localhost shell]# wc -l 0< filefile
  2. 5

9、

  1. [root@localhost shell]# cat hello.c 1>> filefile  #在标准输出结果后面追加内容
  2. [root@localhost shell]# cat filefile
  3. #include
  4. main()
  5. {
  6. printf("HELLP\n");
  7. }
  8. #include
  9. main()
  10. {
  11. printf("HELLP\n");
  12. }

10、

  1. [root@localhost shell]# cat newfile 
  2. if
  3. if2
  4. if_exam
  5. if_exam1
  6. [root@localhost shell]# cat >> newfile << good  #提示若输入good则结束输入编辑
  7. > this is a file
  8. > very good
  9. > good
  10. [root@localhost shell]# cat newfile
  11. if
  12. if2
  13. if_exam
  14. if_exam1
  15. this is a file
  16. very good


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