linux Bash Shell IO重定向与管道

1. 输入与输出

  • 标准输入 STDIN
    文件描述符:0,默认:键盘输入
  • 标准输出 STDOUT
    文件描述符:1,默认:屏幕输出
  • 错误输出 STDERR
    文件描述符:2,默认:屏幕输出

2. 标准输出重定向

  • 覆盖输出 >

  • 追加输出 >>

注意:shell的内嵌命令set可以设置是否允许输出重定向至已存在的文件

set -C:禁止输出重定向至已存在的文件
set +C:允许输出重定向至已存在的文件
示例:标准输出重定向到文件(实际动作:先创建文件,再向其中写入标准输出内容)

[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, sh
[root@VM_41_201_centos ~]# ls -m > ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
[root@VM_41_201_centos ~]# ls -m >> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
anaconda-ks.cfg, ls.txt, sh

3. 错误输出重定向

  • 覆盖输出 2>

  • 追加输出 2>>

示例:错误输出重定向到文件

[root@VM_41_201_centos ~]# lss 2> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# lss 2>> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令

4. 合并标准输出与错误输出重定向

覆盖输出 &>

COMMAND &> 文件  
COMMAND > 文件 2>&1    #不推荐这种形式,难记,不好理解
COMMAND > 文件A 2> 文件B
追加输出 &>>
COMMAND &>> 文件  
COMMAND >> 文件 2>&1    #不推荐这种形式,难记,不好理解
COMMAND > 文件A 2> 文件B
示例:合并标准输出与错误输出重定向

# 标准输出
[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, ls.error, ls.txt, sh

# 标准输出重定向到文件
[root@VM_41_201_centos ~]# ls -m &> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh

# 错误输出重定向到文件(追加)
[root@VM_41_201_centos ~]# lss &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
-bash: lss: 未找到命令

# 合并标准输出与错误输出(标准输出,覆盖)
[root@VM_41_201_centos ~]# ls -m > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh

# 合并标准输出与错误输出(错误输出,覆盖)
[root@VM_41_201_centos ~]# lss > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令

# 合并标准输出与错误输出(错误输出,追加)
[root@VM_41_201_centos ~]# lsss >> ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令

# 合并标准输出与错误输出(错误输出,追加 ,另一种方式)
[root@VM_41_201_centos ~]# lsls &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令
-bash: lsls: 未找到命令
[root@VM_41_201_centos ~]#

#使用COMMAND > 文件A 2> 文件B、COMMAND >> 文件A 2>> 文件B的形式
[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg  ls.error  ls.txt  sh  tee.txt
[root@VM_41_201_centos ~]# ls -m > A 2> B
[root@VM_41_201_centos ~]# ls -m
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat A
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat B
[root@VM_41_201_centos ~]# sl -m > A 2> B
[root@VM_41_201_centos ~]# cat A
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
[root@VM_41_201_centos ~]# lss -m >> A 2>> B
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
-bash: lss: 未找到命令

5. 输入重定向

输入重定向相比较而言,就比较简单了,而且用得相对较少

一般用法:将文件作为输入重定向到命令

命令 < 文件
示例:将文件输入重定向至命令wc统计文件行数:

[root@VM_41_201_centos ~]# wc -l < anaconda-ks.cfg
148

6. 管道

管道用于连接多个命令(程序),将前一个命令的结果重定向,作为后一个命令的输入

COMMAND1 | COMMAND2 | COMMAND3 | ...
示例:将文件内容通过管道重定向到命令(其效果类似输入重定向)
[root@VM_41_201_centos ~]# cat anaconda-ks.cfg | wc -l
148

7. 相关命令

以下几个命令经常与IO重定向(> >> 2> 2>> &> &>> )管道(|)结合使用

  • tee
    read from standard input and write to standard output and files

tee命令比较特殊:从标准输入读取、写入标准输出、写入文件(同时干了3件事,一箭三雕),IO重定向与tee更配哦。

示例:tee命令(执行结果既输出到了指定文件,又输出到了terminal)

[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg  ls.error  ls.txt  sh
[root@VM_41_201_centos ~]# ls -m | tee tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]# cat tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]#
  • tr - translate or delete characters

tr命令用于替换及删除字符

SYNOPSIS

tr [OPTION]... SET1 [SET2]
OPTIONS
-c, -C, --complement
    use the complement of SET1

-d, --delete
    delete characters in SET1, do not translate

-s, --squeeze-repeats
    replace each input sequence of  a  repeated  character  that  is listed in SET1 with a single occurrence of that character

-t, --truncate-set1
    first truncate SET1 to length of SET2
示例
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# cat ls.error | tr b B
-Bash: lss: 未找到命令
-Bash: lss: 未找到命令
  • 输入重定向分界符:<<

允许用户一直输入,直到输入的内容匹配<<指定的字符串

# EOF可以用其它字符代替,习惯用EOF,end of file
命令 << EOF
示例:
[root@VM_41_201_centos ~]#
[root@VM_41_201_centos ~]# cat << EOF
> 1.a
> 2.b
> 3.c
> EOF
1.a
2.b
3.c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值