[Tutorials] 简说输出重定向
Tofloor
poster avatar
没事儿瞎转悠
deepin
2023-12-14 07:27
Author

文件描述符

Linux进程的I/O操作都是在读取或写入一个文件描述符(File Descriptor)。

文件描述符是一个和进程正使用的文件相关联的非负整数。

Linux一切皆文件,所以文件描述符指向的可能是普通文件,也可能是设备如键盘、显示器,也可能是管道、网络链接等等。

进程启动时有3个默认的文件描述符,如下:

文件描述符 中文名称 英文名称 默认链接
0 标准输入 stdin 终端输入设备(键盘)
1 标准输出 stdout 终端显示设备 (显示屏)
2 标准错误输出 stderr 终端显示设备 (显示屏)

重定向

进程(命令)从它的“标准输入”流入数据,正确结果通过“标准输出“流出,错误结果通过“标准错误输出“流出。

输入输出重定向就是将进程的标准输入(0)、标准输出(1)、标准错误输出(2)的默认链接,变更为到其他文件的链接。

重定向一般用法:

用法 描述
command > file1 重定向 标准输出,覆盖到文件file1。等价于 command 1> file1
command >> file1 重定向 标准输出,追加到文件file1。等价于 command 1>> file1
command 2> file1 重定向 标准错误输出,覆盖到file1
command 2>> file1 重定向 标准错误输出,追加到file1
command 2> /dev/null 重定向 标准错误输出,到 /dev/null丢弃。
command 1> file1 2>&1 重定向 标准输出覆盖到到file1,重定向标准错误输出覆盖到标准输出相同的位置。
command 1>> file1 2>&1 重定向标准输出追加到文件file1,重定向标准错误输出到标准输出相同的位置。
command &> file1 使用合并重定向操作符,重定向标准输出和标准错误输出。
command < file1 重定向 标准输入来自file1。等价于command 0< file1

合并重定向操作符优点是简化了写法,缺点是在某些情况有兼容性问题

管道

管道符“|”,将1个命令的标准输出链接到下一个命令的标准输入。
数据通过管道从一个进程流向另一个进程,并被流过的每个进程所改变。

例如:ls -il | wc |less

重定向和管道同时使用

重定向将标准输出发送到文件或从文件获取标准输入。
管道将一个进程的标准输出链接到另一个进程的标准输入。

重定向和管道同时使用时:

cat /etc/passwd >passwd.out | wc
如果重定向到指定文件发生在管道符之前,则管道符之后的命令获取的标准输入为空。因为管道符是将标准输出定向到下一个程序的标准输入,而标准输出已经被重定向到文件了。

cat /etc/passwd |wc >passwd.out
如果管道位于重定向到指定文件之前,则重定向的标准输出是管道之后命令的标准输出,而不是管道之前命令的。

如果需要同时将标准输出保存到文件,并且给下一个命令用作输入,可以使用tee命令。

tee命令从标准输入读取并写入标准输出和文件。

ls /etc | tee etc.list | less
ls /etc的输出覆盖到etc.list文件,并同时用less打开。

ls /etc | tee -a etc.list
ls /etc/的输出追加到etc.list,并同时在屏幕显示。

如果将标准输出和标准错误输出都流入管道,只需将错误输出重定向到标准输出的位置即可。command 2>&1 | command(此种情况不可以使用合并重定向操作符)

示例

标准输出(1)重定向:

user1@user1-PC:~$ date
2023年 12月 14日 星期四 12:19:06 CST              # date命令的标准输出
user1@user1-PC:~$ date >date.txt;wc date.txt;cat date.txt
 1  6 43 date.txt                                # 输出重定向到date.txt,wc命令读取文件内容
2023年 12月 14日 星期四 12:19:21 CST              # cat命令读取date.txt 文件内容
user1@user1-PC:~$ date 1>>date.txt;wc date.txt;cat date.txt
 2 12 86 date.txt                                # 重定向追加到文件,文件内容2行
2023年 12月 14日 星期四 12:19:21 CST              # 文件内容第1行
2023年 12月 14日 星期四 12:19:34 CST              # 文件内容第2行
user1@user1-PC:~$ date 2>>date.txt;wc date.txt;cat date.txt
2023年 12月 14日 星期四 12:19:58 CST              # 错误消息重定向到文件,正确消息输出到屏幕
 2 12 86 date.txt                                # 没有错误消息,文件内容仍为2行
2023年 12月 14日 星期四 12:19:21 CST              # 文件内容第1行
2023年 12月 14日 星期四 12:19:34 CST              # 文件内容第2行

标准错误输出(2)重定向:

user1@user1-PC:~$ asdf
-bash: asdf:未找到命令                         # 输出错误消息
user1@user1-PC:~$ asdf 2> /dev/null            # 错误消息输出到/dev/null丢弃。
user1@user1-PC:~$ asdf 1> asdf.txt;wc asdf.txt;cat asdf.txt
-bash: asdf:未找到命令                         # 错误消息没有被重定向到文件,屏幕输出
0 0 0 asdf.txt                                 # 文件无内容
user1@user1-PC:~$ asdf 2> asdf.txt;wc asdf.txt;cat asdf.txt
 1  2 30 asdf.txt                              # 错误消息定向到文件 
-bash: asdf:未找到命令                         # 文件内容
user1@user1-PC:~$ asdf 2>> asdf.txt;wc asdf.txt;cat asdf.txt
 2  4 60 asdf.txt                              # 追加后文件内容为2行
-bash: asdf:未找到命令                         # 文件内容第1行
-bash: asdf:未找到命令                         # 文件内容第2行

标准输入重定向

user1@user1-PC:~$ wc /etc/passwd
  42   73 2543 /etc/passwd                     # 未重定向,输出包含文件名
user1@user1-PC:~$ wc 

tee命令

user1@user1-PC:~$ date | tee date.txt ;wc date.txt;cat date.txt
2023年 12月 14日 星期四 12:36:56 CST            # tee命令的标准输出到显示屏
 1  6 43 date.txt                              # 内容同时到作为参数的文件date.txt
2023年 12月 14日 星期四 12:36:56 CST            # date.txt内容和标准输出内容相同
user1@user1-PC:~$ date | tee date.txt|wc ;wc date.txt;cat date.txt
      1       6      43                        # tee命令标准输出通过管道作为wc的输入
 1  6 43 date.txt                              # wc命令读取date.txt文件
2023年 12月 14日 星期四 12:37:27 CST            # date.txt文件内容
user1@user1-PC:~$ date | tee date.txt >date2.txt ;wc date.txt date2.txt;cat date.txt date2.txt
 1  6 43 date.txt                              # 作为tee命令参数的date.txt获取到内容
 1  6 43 date2.txt                             # tee标注输出重定向到文件date2.txt
 2 12 86 总用量
2023年 12月 14日 星期四 12:38:01 CST            # date.txt文件内容
2023年 12月 14日 星期四 12:38:01 CST            # date2txt文内容和date.txt文件相同

Reply Favorite View the author
All Replies
fslong
deepin beta test group
2023-12-14 07:36
#1

好帖子,支持楼主

Reply View the author
云的眼泪
deepin
2023-12-14 16:17
#2

学习学习

Reply View the author
阿尼樱奈奈
Moderator
2023-12-14 16:33
#3
like
Reply View the author
jjcui8595
deepin
2023-12-14 17:50
#4

感谢分享

Reply View the author
fax928
deepin
2023-12-14 18:04
#5

like

Reply View the author
yanjuner
Super Moderator
2023-12-14 18:21
#6

好内容

Reply View the author
babyfengfjx
Super Moderator
CQA
2023-12-14 21:16
#7

学习了👍

Reply View the author