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
相关文章推荐
- Linu Shell 编程基础--基本IO重定向和管道
- IO重定向及管道操作
- SHELL重定向和管道的实现
- 从windows到*nix(五) IO重定向与管道
- shell之重定向和管道
- linux_IO重定向以及管道
- C语言父子进程shell命令重定向到管道传递(双管道)
- Shell 编程11(IO重定向)
- shell基础-输入输出以及重定向/多命令顺序执行与管道符/通配符与特殊符号
- Shell基础:介绍、历史命令、命令不全和别名、通配符、输入输入重定向、管道发和作业控制
- shell管道与重定向
- shell变量、重定向、管道
- Shell脚本编程之Bash特性-IO重定向-变量
- echo,Shell管道与重定向
- 2.2-IO重定向,管道及文本处理工具
- linux shell 管道命令(pipe)使用及与shell重定向区别
- linux-IO重定向及管道,tr、tee
- shell重定向,管道
- linux shell 管道命令(pipe)使用及与shell重定向区别
- shell脚本之重定向与管道(三)