管道、Bshe重定向简述

    管道 " | "

    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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值