您的位置:首页 > 运维架构 > Shell

linux Bash Shell IO重定向与管道

2017-12-11 17:09 489 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: