linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&< 的含义及使用介绍

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&< 的含义及使用介绍

一、标准输入输出

执行一个shell命令行时通常会自动打开三个标准文件

描述符0:标准输入文件(stdin),通常对应终端的键盘;

描述符1:标准输出文件(stdout);

描述符2:标准错误输出文件(stderr)。

二、重定向 

重定向:改变标准输入/输出的方向

有时候我们不想把一些进程处理后信息输出到显示器,这时我们就引出了重定向。

 

 三种重定向:

1、重定向标准输出,包括两种。

1)>(覆盖),等价1>,将命令执行的结果输出到指定文件,非显示器。

[root@dream dream]# echo -e "123456\n123456"
123456
123456
[root@dream dream]# echo -e "123456\n123456" > passwd.txt
[root@dream dream]# cat passwd.txt
123456
123456

2)>>(追加),等价1>>,将命令执行的结果追加到指定文件,非显示器。

[root@dream dream]# cat passwd.txt 
123456
[root@dream dream]# echo -e "123456" >> passwd.txt #使用>>进行追加
[root@dream dream]# cat passwd.txt 
123456
123456
[root@dream dream]# echo -e "123456" 1>> passwd.txt  #使用1>>进行追加
[root@dream dream]# cat passwd.txt 
123456
123456
123456
[root@dream dream]# 

2、 重定向标准输入,包括两种。

1)<:将命令中接收的输入途径,由键盘改为指定文件。

[root@dream dream]# cat passwd.txt
123456
123456
[root@dream dream]# useradd demo   #创建用户
[root@dream dream]# passwd demo < passwd.txt  #设置用户密码,从文件passwd.txt中读取
Changing password for user demo.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@dream dream]# 

2)<<[Here Document]:命令序列传递到一个交互程序或者命令中。

[root@dream dream]# passwd demo << EOF
> abcdef
> abcdef
> EOF
Changing password for user demo.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@dream dream]# 

3、 重定向标准错误,包括两种。

1)2>(覆盖),将命令执行的结果输出到指定文件。

[root@dream dream]# ll noFile.txt 2> errFile.txt  #将错误信息输入到errFile.txt文件中
[root@dream dream]# cat errFile.txt 
ls: cannot access noFile.txt: No such file or directory
[root@dream dream]#

2)2>>(追加),将命令执行的结果追加到指定文件。

[root@dream dream]# cat errFile.txt 
ls: cannot access noFile.txt: No such file or directory

[root@dream dream]# ll noFile.txt 2>> errFile.txt  #追加

[root@dream dream]# cat errFile.txt 
ls: cannot access noFile.txt: No such file or directory
ls: cannot access noFile.txt: No such file or directory
[root@dream dream]# 

 

三、补充

1、重定向标准输出和重定向标准错误到同一个文件中

有以下的几种方式:

  • 2>&1
  • >&
  • &>

2、两个特殊文件

1)/dev/nul:过滤标准错误信息,意思就是不想显示结果就输出到这里面。

2)/dev/zero:创建指定长度的文件 

四、案例

1、>&  重定向标准输出和重定向标准错误到同一个文件中,即正确的和错误的都输出到一个文件中。

[root@dream dream]# touch passwd.txt
[root@dream dream]#  ll passwd.txt passwd1.txt &> one.txt  //查看两个文件,passwd.txt存在,passwd1.txt不存在
[root@dream dream]# cat one.txt //查看输出,一条成功,一条错误
ls: cannot access passwd1.txt: No such file or directory
-rw-r--r-- 1 root root 0 Dec 31 19:35 passwd.txt
[root@dream dream]# 

2、&> 与>&一样,重定向标准输出和重定向标准错误到同一个文件中,即正确的和错误的都输出到一个文件中。

[root@dream dream]# touch passwd.txt
[root@dream dream]#  ll passwd.txt passwd1.txt >& one.txt  //查看两个文件,passwd.txt存在,passwd1.txt不存在
[root@dream dream]# cat one.txt //查看输出,一条成功,一条错误
ls: cannot access passwd1.txt: No such file or directory
-rw-r--r-- 1 root root 0 Dec 31 19:38 passwd.txt
[root@dream dream]# 

3、2>&1,与>&和&>效果一样,该命令是将错误2的信息合并到成功1的信息一起输出。

[root@dream dream]# touch passwd.txt
[root@dream dream]# ll passwd.txt passwd1.txt > file 2>&1 //ll执行的结果输出到file中
[root@dream dream]# cat file  //查看输出
ls: cannot access passwd1.txt: No such file or directory
-rw-r--r-- 1 root root 0 Dec 31 19:40 passwd.txt
[root@dream dream]# 

4、使用/dev/nul文件

有时候我们使用命令,不想将输出的信息显示到界面,这时我们可以使用 &> /dev/nul[常用]

[root@dream dream]# echo "123456" | passwd --stdin demo  //修改demo的密码,执行完会提示一些信息。
Changing password for user demo.
passwd: all authentication tokens updated successfully.
[root@dream dream]# echo "123456" | passwd --stdin demo &> /dev/nul //使用&> /dev/nul后不再提示信息。
[root@dream dream]# 
posted @ 2019-12-31 19:49  航松先生  阅读(1)  评论(0)    收藏  举报