点我

输入输出重定向

简而言之,输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。在日常的学习和工作中,相较于输入重定向,我们使用输出重定向的频率更高,所以又将输出重定向分为了标准输出重定向和错误输出重定向两种不同的技术,以及清空写入与追加写入两种模式。

  • 标准输入重定向(STDIN,文件描述符为0):默认从键盘输入,也可以从其他文件或者命令中输入。
  • 标准输出重定向(STDIN,文件描述符为1):默认输出到屏幕上。
  • 错误输出重定向(STDIN,文件描述符为2):默认输出到屏幕。

标准输入重定向

输入重定向中用到的符号及其作用

符号作用
命令 < 文件将文件作为命令的标准输入
命令 < 文件1 > 文件2将文件1作为命令的标准输入并将标准输出到文件2
[root@bogon test]# cat < exec.txt 
pwd
[root@bogon test]# cat exec.txt 
pwd
[root@bogon test]# 
[root@bogon test]# cat < exec.txt > rs.txt
[root@bogon test]# cat rs.txt 
pwd
[root@bogon test]# ll
总用量 8
-rw-r--r--. 1 root root 4 12月 29 02:17 exec.txt
-rw-r--r--. 1 root root 4 12月 29 02:21 rs.txt
[root@bogon test]# 

标准输出重定向

常用参数

参数作用
命令 > 文件将标准输出重定向到一个文件中(清空原来文件的数据)
命令 2> 文件将错误输出重定向到一个文件中(清空原来文件的数据)
命令 >> 文件将标准输出重定向到一个文件中(追加到原来文件数据的后面)
命令 2>> 文件将错误输出重定向到一个文件中(追加到原来文件数据的后面)
命令 >> 文件 2>&1将标准输出与错误输出同时一个到一个文件中(追加到原来文件数据的后面)
命令 &>> 文件将标准输出与错误输出同时一个到一个文件中(追加到原来文件数据的后面)
[root@bogon test]# echo "xbxaq.com" > res.txt
[root@bogon test]# cat res.txt 
xbxaq.com
[root@bogon test]# echo "hello" > res.txt
[root@bogon test]# cat res.txt 
hello
[root@bogon test]# echo "xbxaq.com" >> res.txt
[root@bogon test]# cat res.txt 
hello
xbxaq.com
[root@bogon test]# 
[root@bogon test]# ifcon > res.txt 
bash: ifcon: 未找到命令...
[root@bogon test]# ifcon 2> res.txt 
[root@bogon test]# cat res.txt 
bash: ifcon: 未找到命令...
[root@bogon test]# ifco123123n 2>> res.txt 
[root@bogon test]# cat res.txt 
bash: ifcon: 未找到命令...
bash: ifco123123n: 未找到命令...
[root@bogon test]# 

分类: Linux

评论

-- 评论已关闭 --

目录