Linux的输出、输入重定向和管道

目录

输出重定向

输入重定向

<

<<

管道操作


输出重定向

当我输⼊⼀个命令之后,回⻋,命令产⽣了结果,结果默认是输出到屏幕上的。

默认情况,⽆论⼀个命令执⾏正确与否,结果都会默认输出到屏幕上。

在有些情况下,我可能需要保留命令或脚本输出的结果。当作log,用作后面分析。

cat /cat /etc/hosts 产生的结果是正确的,我们可以使用 >>> 见这个命令的正确结果输出到一个文件中

bash 复制代码
[root@bogon ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@bogon ~]# cat /etc/hosts > /root/file1
[root@bogon ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@bogon ~]# cat file1 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

cat /etc/hosts > /root/file1 就是将 > 前面命令的结果送到 /root/file1 文件中,而不是屏幕上。

  • >>>:是正确的重定向,能将正确的结果重定向到⽂件中,区别是>会将指向的⽂件内容完全覆盖>>会将重定向的内容追加到指定的⽂件,>就是正确的覆盖,>>就是正确的追加

  • 2>:就是错误的覆盖

  • 2>>:就是错误的追加

  • &>:正确错误都覆盖

  • &>>:正确错误都追加

  • &>> /dev/null/dev/null是⼀个特殊的⽂件,如果将重定向指向这个⽂件,那么就相当于将执⾏结果送到⿊洞中。直接没了,有些时候,我们不想让这个命令的输出显示到任何地⽅,就送到⿊洞中。

输入重定向

可以将交互式命令变成非交互式命令

<

<<<的作用完全不一样

< 可以将<前命令需要执行后产生需要进行交互的内容,通过后面文本中预先写好的内容进行操作,这个看起来很是鸡肋,但是在编写shell脚本是不可缺少的。

为了刚明显的看出,举个实例:

bash 复制代码
## 编辑一个1.txt文件,在里面写入一些内容
[root@bogon ~]# cat 1.txt 
helld world!!!

## 在2.txt写入y,表示yes
[root@bogon ~]# echo y > 2.txt
[root@bogon ~]# cat 2.txt 
y

## 编辑3.txt文件,在里面写一些不同于1.txt的内容
[root@bogon ~]# cat 3.txt 
I love the world!!!

## 将1.txt文件中的内容覆盖到3.txt文件中,会显示"cp: overwrite '3.txt'? ",只需要输入y,就能将1.txt的内容已经覆盖3.txt文件
[root@bogon ~]# cp 1.txt 3.txt
cp: overwrite '3.txt'? y
[root@bogon ~]# cat 3.txt 
helld world!!!

## 重新对3.txt进行编写
[root@bogon ~]# cat 3.txt 
I love the world!!!

## 如果使用输入重定向,就可以不用手动输入y,可以将2.txt中的内容,进行输出,直接完成结果
[root@bogon ~]# cp 1.txt 3.txt < 2.txt 
cp: overwrite '3.txt'? [root@bogon ~]# cat 3.txt 
helld world!!!

<<

一般是和cat进行连用

当执行一个单个的cat时候,会进入到交互界面,退出时执行使用 Ctrl+C 进行终止,但是使用 << 就可以手动进行终止

<<后面写入的内容,会作为结束的标志,进行退出

bash 复制代码
[root@bogon ~]# cat  << qqq
> nihao 
> hello 
> aaaa 
> adsad 
> qqq
nihao 
hello 
aaaa 
adsad 
[root@bogon ~]# 

<<后面些qqq之后,在cat界面,输入qqq之后,就会执行出现在前方写入的内容,紧接着就会退出cat的交互式界面

这种方式多使用在脚本编写中,可以将内容输入到指定的文件中,一般情况下<<之后写的是END,这里便于理解,就随便写了一串字母

bash 复制代码
[root@bogon ~]# cat > 4.txt << ooo
> aaaa
> bbbb
> ccccasd
> aasdexffd
> ooo
[root@bogon ~]# cat 4.txt 
aaaa
bbbb
ccccasd
aasdexffd
[root@bogon ~]# 

管道操作

管道的符号是 |

⽂件管理⾥⾯⽐较重要的内容

管道左边的命令会产⽣输出结果,输出结果经过了管道之后,就会变成输⼊。

管道右边的命令,总是接收输⼊的命令

bash 复制代码
[root@bogon ~]# cat hello.txt 
How are you?
what are you doing?
##  查看hello.txt中的内容,并把所有字母转变为大写
[root@bogon ~]# cat hello.txt | tr 'a-z' 'A-Z'
HOW ARE YOU?
WHAT ARE YOU DOING?
## 转变为大写之后,输出,并保存到hello2.txt文件中
[root@bogon ~]# cat hello.txt | tr 'a-z' 'A-Z' | tee hello2.txt
HOW ARE YOU?
WHAT ARE YOU DOING?
[root@bogon ~]# cat hello2.txt 
HOW ARE YOU?
WHAT ARE YOU DOING?
## 查找hello2.txt中有HOW的内容
[root@bogon ~]# cat hello2.txt | grep HOW
HOW ARE YOU?
## 显示有HOW内容的行数
[root@bogon ~]# cat hello2.txt | grep HOW | wc -l
1
相关推荐
A小辣椒1 小时前
TShark:基础知识
linux
AlfredZhao3 小时前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao18 小时前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334661 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪1 天前
linux 拷贝文件或目录到指定的位置
linux
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush42 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5202 天前
Linux 11 动态监控指令top
linux