linux io 重定向 符号,Linux中基本I/O 重定向的符号及其用法和文件标识符

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选项开启,仍然强制将FD为n的输出写到filename中,即把filename内容覆盖掉

7

n> filename

将FD为n的输出写到filename文件中

8

n< filename

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

9

n>> filename

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

10

<< delimiter

此处文档(Here——document)

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

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

举例1、

[root@localhost shell]# ls -al |wc -l

95

2、

[root@localhost shell]# cat >newfile #按住Ctrl+D结束编辑

hello this is cherishyou's blog

you can find some my dialog

see you [root@localhost shell]#

[root@localhost shell]# cat newfile

hello this is cherishyou's blog

you can find some my dialog

see you

3

[root@localhost shell]# wc < newfile #将newfile中的统计输出

2 13 68

4、

[root@localhost shell]# ls /home/cherish/shell/ifexample/ >>newfile #在newfile文件后面追加内容

[root@localhost shell]# cat newfile

hello this is cherishyou's blog

you can find some my dialog

see you if

if2

if_exam

if_exam1

if_file1

ifcopy

ifeldel

ifelifeise

ifelse

ifelse1

ifelse2

ifelse3

5、

[root@localhost shell]# set -o noclobber  #开启noclobber(不允许覆盖任何文件)

[root@localhost shell]# ls /home/cherish/shell/ifexample/ > newfile  #出现错误

-bash: newfile: cannot overwrite existing file

[root@localhost shell]# ls /home/cherish/shell/ifexample/ >| newfile  #正常运行 !!注意区别

[root@localhost shell]# cat newfile

if

if2

if_exam

if_exam1

if_file1

ifcopy

ifeldel

ifelifeise

ifelse

ifelse1

ifelse2

ifelse3

6、

[root@localhost shell]# ls z*  #无法访问

ls: cannot access z*: No such file or directory

[root@localhost shell]# ls z* >filefile   #出现错误

ls: cannot access z*: No such file or directory

[root@localhost shell]# cat filefile

[root@localhost shell]# ls z* 2> filefile  #保存错误信息

[root@localhost shell]# cat filefile

ls: cannot access z*: No such file or directory

7、

[root@localhost shell]# cat hello.c 1> filefile  #显示标准输出结果

[root@localhost shell]# cat filefile

#include

main()

{

printf("HELLP\n");

}

8、

[root@localhost shell]# wc -l 0< filefile

5

9、

[root@localhost shell]# cat hello.c 1>> filefile  #在标准输出结果后面追加内容

[root@localhost shell]# cat filefile

#include

main()

{

printf("HELLP\n");

}

#include

main()

{

printf("HELLP\n");

}

10、

[root@localhost shell]# cat newfile

if

if2

if_exam

if_exam1

[root@localhost shell]# cat >> newfile << good  #提示若输入good则结束输入编辑

> this is a file

> very good

> good

[root@localhost shell]# cat newfile

if

if2

if_exam

if_exam1

this is a file

very good

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值