管道 " | "
bash重定向
1、管道(使用符号"|"表示)用来连接命令
command1 | command2|command3|…
管道默认不不接受错误重定向,可使用如下方式重定向错误输出
#默认不允许管道错误重定向
[root@centos7 ~]# echoo "abc"| echo "ab"
ab
bash: echoo: command not found...
Similar command is: 'echo'
(1)
[root@centos7 ~]# echoo "adb" 2>&1|echo abc
abc
(2)
[root@centos7 ~]# echo "abc" |& echo "ab"
ab #& 是一个描述符,如果不加&,会被当成一个普通文件。
2、bash重定向
> 标准正确重定向
2> 标准错误重定向
&> 所有输出重定向(包括正确和错误)
> 覆盖元数据
>> 追加数据
() 合并多个程序的STDOUT
set -C 禁止覆盖
[root@centos7 ~]# set -C f9
[root@centos7 ~]# echo 222 > f9
-bash: f9: cannot overwrite existing file
\>| fiel 强制覆盖
[root@centos7 ~]# set -C f9
[root@centos7 ~]# echo 222 > f9
-bash: f9: cannot overwrite existing file
[root@centos7 ~]# echo 333 >| f9
[root@centos7 ~]# cat f9
333
set +C 解锁覆盖
[root@centos7 ~]# set +C f9
[root@centos7 ~]# echo 111 > f9
[root@centos7 ~]# cat f9
111
文件重定向示例:
[root@centos8 ~]#ls /data /xxx > /data/all2.log 2>&1
#把错的放到正确的里面,再把正确的放入all2.log,即 &> all2.log
[root@centos8 ~]#ls /data /xxx 2> /data/all4.log 1>&2
#把正确的放入错的中,再把错的放入all4.log中
[root@centos8 ~]#ls /data /xxx &> /data/all.log
#所有输出都放入all.log中
[root@centos8 ~]#ls /data /xxx 2>&1 > /data/all3.log
ls: cannot access '/xxx': No such file or directory
#安装命令的执行测序,前面的已经执行,再追加已经晚了
注: 标准错误输出不一定时错误的,如一些特殊命令,根据系统定义。
STDIN标准输入重定向
< : 重定向标准输入,可以从文件中导入标注输入
tr 'a-z' 'A-Z' < /etc/issue
cat < file1 > file2 把file1的内容标准输入覆盖f2
cat < file1> >> file2 file1追加到f2
多行重定向:<< 终止词
[root@centos7 testdir]# cat << EOF
a
b
c
EOF
a
b
c
管道中 - 取前一个命令的标准输出
[root@centos7 testdir]# cat f1 | file -
/dev/stdin: ASCII text
tee重定向到多个目标
command1 | tee [-a] filename|command2
-a 追加不覆盖
[root@centos7 ~]# echo a |tee f8 |cat
a