Unix中的输入输出重定向

写这篇是因为在写bash命令时,总是搞混,有时候就是干脆不懂定向,结合菜鸟教程里的讲解学习,

命令 说明
command > file 将输出重定向到 file。
command < file 将输入重定向到 file。
command >> file 将输出以追加 append 的方式重定向到 file。
n > file 将文件描述符为 n 的文件重定向到 file。
n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m 将输出文件 m 和 n 合并。
n <& m 将输入文件 m 和 n 合并。
<< tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

什么是文件描述符?
0, 1, 2…9 are file descriptors in bash.

0 stands for stdin, 1 stands for stdout, 2 stands for stderror. 3~9 is spare for any other temporary usage.

Any file descriptor can be redirected to other file descriptor or file by using operator > or >>(append).

Usage: <file_descriptor> > <filename | &file_descriptor>

Please reference to http://www.tldp.org/LDP/abs/html/io-redirection.html
你输出至stdout,其实就是输出至/dev/stdout 也就是文件描述符1.
/dev/stdout is a device file, which is a link to /proc/self/fd/1, which means it is referring to the file descriptor 1 held by the current process;/dev/stdout is a link to a special file on the procfs file system, which represents file descriptor 1 of the process using it.

Hence it has a very real connection to the process standard output.

The procfs file system holds all sorts of wondrous information about the system and all its processes (assuming a process has permissions to get to them, which it should have for /proc/self).

第四个之后都看不太明白了,需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

1、输出重定向
最常用的,例如echo “hello world” > 1.txt 这种的。输出日志也常用这个。这个命令执行command1然后将输出的内容存入file1.注意任何file1内的已经存在的内容将被新内容替代。如果要将新内容添加在文件末尾,请使用>>操作符。

 >a1  等同于 1>a1

2、输入重定向
这个用的应该稍少一点,记得是看一个视频课的时候看老师用这个,那个时候刚用linux没多久,第一反应还以为老师写错了,不过那个时候也不是很懂,
command1 < file1
这样,本来需要从键盘获取输入的命令会转移到文件流式读取内容。
其实 如果command1 都是操作文件的指令话,command1 file1 和 command1 < file1还比较像,但是后者是把文件的内容作为输入,因为它仅仅知道从标准输入读取内容,是有区别的。如
wc -l file.txt 和 wc -l < file.txt

3、command1 < infile > outfile

同时替换输入和输出,执行command1,从文件infile读取内容,然后将输出写入到outfile中。
确实少见,但也够骚

sed 's/hello/world/' < input.txt > output.txt

重定向深入讲解
标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

command > file 就是将本应输出至 stdout 重定向到 输出到file,command < file 将stdin 重定向到 file文件流。

如果希望 stderr 重定向到 file,可以这样写:
command 2 > file 就是把报错信息写入file中。

一般当我们用 > console.txt,实际是 1>console.txt的省略用法;
< console.txt ,实际是 0 < console.txt的省略用法。

如果希望将 stdout 和 stderr 合并后重定向到 file,可以这样写:
command > file 2>&1

这个写法是什么意思呢?
放在>后面的&,表示重定向的目标不是一个文件,而是一个文件描述符,就是前面的0,1,2,如果是2>1那就表示是把stderr 写在目录下名称叫 1 的文件里(如果没有,将被创建)
2>&1其实就是把所有的log输出都写进file里了,查看的时候比较方便。
2>&1代表将stderr重定向到文件描述符为1的文件(即/dev/stdout)中,所以还有这样写的

make 2>&1 | tee err
vim err

tee这个命令可以让输出的信息也同时能写入文件,这里就是把输出都写进err

file 2>&1 与 &>file 这两个是等价的,作用相同。具体看https://stackoverflow.com/questions/876239/how-to-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash

Here document

这个是linux中的一种特殊的重定向方式,

command << delimiter
    document
delimiter

delimiter这里常用EOF。(EOF (end-of-file) is a condition encountered by a read() operation on a file, terminal or file-like data stream (socket). It is not a character.)其实就是把document的内容重定向成输入给command处理了。在shell脚本见过这类,本文就不细究了,放一个用法链接:https://www.cnblogs.com/mkdlf/p/7445822.html
https://blog.csdn.net/liumiaocn/article/details/86715953
https://tldp.org/LDP/abs/html/here-docs.html

/dev/null 相关

/dev/null 文件
如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

$ command > /dev/null
/dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到"禁止输出"的效果。

如果希望屏蔽 stdout 和 stderr,可以这样写:

$ command > /dev/null 2>&1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值