linux中的重定向符和管道(<,><<,>>,|),作用:将一个命令的标


管道与重定向的使用

 

标准输入的文件描述符为0.

 

标准输出的文件描述符为1

 

错误输出的文件描述符为2

 

管道

管道:可以让我们将多条命令连接在一起。

作用:将一个命令的标准输出重定向给下一个命令,并作为该命令的标准输入。

(base) [root@localhost ~]# ifconfig ens33|grep 'inet'     过滤包含ip地址的行
        inet 192.168.72.10  netmask 255.255.255.0  broadcast 192.168.72.255
(base) [root@localhost ~]#

 重定向

输出重定向可以使用>或>>符号

> :可以将输出导入到文件,如果文件不存在,则创建该文件,如果文件已经存在,则会覆盖该文件的内容;

>>:可以将输出追加到文件

对于错误信息对的重定向

>2
>>2

代码示例:

(base) [root@localhost ~]# rpm -qa|grep gcc   #查询计算机中是否安装了gcc
(base) [root@localhost ~]# echo "pass" |passwd --stdin yan  #设置yan 的密码为pass
(base) [root@localhost ~]# ls        #查看当前文件列表
anaconda2                           dr-elephant-feature_spark2.3-12.1      node_modules       
(base) [root@localhost ~]# ls > list.txt      #将输出保存至list.txt,屏幕无输出
(base) [root@localhost ~]# hostname >> list.txt #将主机名追加到list.txt文件尾部
(base) [root@localhost ~]# mail -s test xx@gmail.com <list.txt  #发邮件,邮件内容来自list.txt        
(base) [root@localhost ~]# ls -l abc anaconda-ks.cfg  #查看文件详细信息,abc不存在
ls: cannot access abc: No such file or directory
-rw-------. 1 root root 1234 Nov 16  2020 anaconda-ks.cfg
(base) [root@localhost ~]# ls -l abc anaconda-ks.cfg 2>error.txt #仅将错误从定向,不影响输出。
-rw-------. 1 root root 1234 Nov 16  2020 anaconda-ks.cfg
(base) [root@localhost ~]# ls -l abc anaconda-ks.cfg > all 2>&1 #标准输出与错误输出都导入到all
(base) [root@localhost ~]# ls -l abc anaconda-ks.cfg >> all 2>&1 #标准输出与错误输出追加到all
(base) [root@localhost ~]# ls -l abc anaconda-ks.cfg &> all #标准输出与错误输出都导入到all

 

相关内容