美文网首页
Linux-IO重定向与管道

Linux-IO重定向与管道

作者: yuanzicheng | 来源:发表于2017-04-28 17:13 被阅读106次

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
[root@VM_41_201_centos ~]#

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: 未找到命令
[root@VM_41_201_centos ~]#

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: 未找到命令
[root@VM_41_201_centos ~]#

5. 输入重定向

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

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

命令 < 文件

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

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

6. 管道

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

COMMAND1 | COMMAND2 | COMMAND3 | ...

示例:将文件内容通过管道重定向到命令(其效果类似输入重定向)

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

7. 相关命令

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

7.1 tee 从标准输入读取,写入标准输出和文件

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

示例

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 ~]#

7.2 tr - 替换或删除字符

语法

tr [OPTION]... SET1 [SET2]

选项

-c  使用SET1的补集;
-d  删除SET1中的字符;
-s  把连续重复的字符以单独一个字符表示;
-t  替换SET1中SET2长度的字符。

示例

# 将大写字母替换成小写字母
[root@localhost ~]# echo "Hello World" | tr 'A-Z' 'a-z'
hello world
# 删除大写字母
[root@localhost ~]# echo "Hello World" | tr -d [:upper:]
ello orld
# 替换部分字符
[root@localhost ~]# echo "Hello World" | tr -t 'Hello' 'xx'
xxllo World

7.3 输入重定向分界符:<<

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

# 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
[root@VM_41_201_centos ~]#

相关文章

  • Linux-IO重定向与管道

    1. 输入与输出 标准输入 STDIN文件描述符:0,默认:键盘输入 标准输出 STDOUT文件描述符:1,默认:...

  • Windows PowerShell 学习笔记其二(变量与控制语

    重定向与管道 重定向 可以借助管道符和 Out-File 命令将某个命令的输出内容重定向至文本文件中。 如:Get...

  • 第07章重定向管道

    输出重定向案例 > < 脚本中使用重定向 2.输入重定向及结合案例 管道 | 重定向和管道的符号对比。重定向输出到...

  • 管道命令

    参考linux shell 管道命令(pipe)使用及与shell重定向区别、管道命令 管道命令操作符是:”|”,...

  • 命令输入输出格式:【重定向命令 和 管道命令】\【用选项指定】

    【输入输出】格式 - 重定向 和 管道 简介 重定向命令 三大通用格式 重定向命令举例 管道命令 通用格式 管...

  • 重定向

    输出重定向 输入重定向 错误重定向 管道 shell中特殊符号

  • 管道与重定向

    管道与重定向 设置tom的密码为passecho "pass" | passwd --stdin tom 将输出保...

  • 生信人的Linux14-24

    Liunx Day15:管道和重定向 管道和重定向:> < >> << Linux: >:覆盖输出 注意文件/t...

  • windows DOS批处理.bat

    批处理符号简介 回显屏蔽 @ 重定向1 >与>> 重定向2 < 管道符号 | 转义符 ^ 逻辑命令符包括:&、&&...

  • PowerShell入门

    PowerShell快捷键 重定向与管道 重定向:命令的输出保存到文件中,'>'为覆盖,'>>'追加如"Power...

网友评论

      本文标题:Linux-IO重定向与管道

      本文链接:https://www.haomeiwen.com/subject/fqcazttx.html