标准I/O与管道

作者: Aubin | 来源:发表于2017-07-22 09:26 被阅读16次

1. Linux一共三种I/O设备:

标准输入---0(默认接收键盘)
标准输出---1(默认输出到终端)
标准错误---2(默认输出到终端)

  • 标准输出
[root@centos6 ~]# ls ./
anaconda-ks.cfg  Documents  install.log  
  • 标准错误
[root@centos6 ~]# aubin
-bash: aubin: command not found
[root@centos6 ~]# ls ./aubin
ls: cannot access ./aubin: No such file or directory

2. I/O重定向

I/O重定向即改变默认位置
标准输出与标准错误可以重定向到文件
COMMOND 操作符 FILE

  • SHELL重定向的执行顺序
shell单条命令,重定向部分的执行顺序:先<,然后command,最后<和<<
  • > 把标准输出重定向到文件
[root@centos6 app]# cal > cal.log
  • 2> 把标准错误重定向到文件
#错误信息重定向到文件后,屏幕上不在输出错误提示
#
[root@centos6 app]# aubin                      #错误信息
-bash: aubin: command not found
[root@centos6 app]# aubin 2> erroor.log        #错误信息重定向到error.log
[root@centos6 app]# cat erroor.log             #输出error.log
-bash: aubin: command not found
  • &> 把所有输出重定向到文件
    当执行一个命令有标准输出又有错误输出时使用&>
[root@centos6 app]# ls /root/ /ccc >test.log 2>&   #旧写法,2<&只能在最后
[root@centos6 app]# ls -l /root/ /ccc >& f1        #旧写法
[root@centos6 app]# ls -l /root/ /ccc &> f1        #将所有信息重定向到f1文件
[root@centos6 app]# cat f1                         #查看f1文件,有标准输出有错误输出
ls: cannot access /ccc: No such file or directory
-rw-------. 1 root root  1479 Jul 14 11:18 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Jul 14 11:26 Desktop
[root@centos6 app]# ls /root 2>file.log >/dev/file.log     
  • 标准输出与标准错误的转换
#"ls /root"为标准输出,2>file为标准错误重定向。标准输出使用标准错误重定向应报错
# 1>&2将标准输出转为标准错误后,命令不报错
[root@centos6 app]# ls /root 2>file 1>&2
#
#
#"ls /aubin"为标准错误,>file为标准输出重定向。标准错误使用标准输出重定向应报错
# 1>&2将标准错误转为标准输出后,命令不报错
[root@centos6 app]# ls /aubin >file 2>&1
  • 覆盖与追加
    >为覆盖,文件不存在则创建,存在则覆盖
    >>为追加,文件不存在则创建,存在则追加
[root@centos6 app]# ls /boot/  &>> file.log 
[root@centos6 app]# ls /boot/  2>> file.log
  • 禁止覆盖set -C取消进制覆盖set +C
[root@centos6 app]# set -C                           #禁止覆盖
[root@centos6 app]# ls /boot/ > file.log             #文件不存在则创建
[root@centos6 app]# ls /boot/ > file.log             #文件存在不允许覆盖
-bash: file.log: cannot overwrite existing file
[root@centos6 app]# set +C                           #取消禁止
  • 手动强行覆盖
[root@centos6 app]# ls /root/ >| file.log 
  • 重定向优先级
    pwd不会重定向>的优先级高于;
[root@centos6 app]# pwd;whoami >file

调整优先级

[root@centos6 app]# (pwd;whoami) >file
  • 将文件重定向到标准输入
    mila文件重定向到mail的标准输入
[root@centos6 app]# mail -s hello li < mail 
  • 黑洞常用语隐藏全部输出
#忽略标准输出信息
[root@centos6 app]# ls /root/ >/dev/null      
#将标准输出定向到test.lg,错误信息定向到黑洞                   
[root@centos6 app]# ls /root /aubin 2>/dev/null >test.log   
#清空日志文件
[root@centos6 app]# cat /dev/null > file.log 
#修改密码不提示任何信息
[root@centos6 app]# echo 123 | passwd --stdin aubin &> /dev/null

3. I/O字符的删除、替换、缩减

  • 字符的删除、缩减
[root@centos6 app]# tr -d 'A-Z' < issue          #删除A-Z的字符
[root@centos6 app]# tr -dc 'A-Z' < issue         #除A-Z的字符都删除
[root@centos6 app]# tr -s 'ac' < issue           #将连续的a和连续的c压缩成一个
  • 对应转化
tr -t '12345' 'abc'                              #只将一一对应的转换
  • 原字符与替换符对称
[root@centos6 app]# tr '123' 'ABC'               #只要标准输入123替换为ABC
123456
ABC456
  • 原字符多于替换符
[root@centos6 app]# tr '123456' 'ABC'            #没有对应的替换符,按最后一个替换
12345678
ABCCCC78
  • 替换符多于原字符
[root@centos6 app]# tr '123' 'ABCEFG'            #无对应原字符的不替换
123456789
ABC456789
  • 替换文本的重定向
[root@centos6 app]# tr 'a-z' 'A-Z' <issue        #issue重定向到tr中然后替换
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \M
  • 转换字符并重定向到新的文件
[root@centos6 /]# tr 'a-z' 'A-Z' < issue > issue2     
[root@centos6 /]# cat issue2
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \M
[root@centos6 /]# tr ' ' '\n' < issue > issue2 #空格替换成回车
[root@centos6 /]# tr -d 'm-n' < issue > issue2 #删除m-n
[root@centos6 /]# tr -dc 'm-n' < issue > issue2 #除m-n外都删除

3. 重定向到同名文件夹

  • 追加文件不会清空
[root@centos6 app]# tr 'a-z' 'A-z' < issue >>issue
[root@centos6 app]# cat issue 
CentOS release 6.9 (Final)
Kernel \r on an \m
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \
  • 重定向会清空
????http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html
[root@centos6 app]# tr 'A-Z' 'a-z' <issue>issue 
[root@centos6 app]# cat issue
#此时tr命令没有输入所以输出为空,直接空重定向到issue
[root@centos6 app]# tr 'A-Z' 'a-z' >issue
[root@centos6 app]# cat issue

4. 文件重定向到标准输入 <

  • 文件重定向到标准输入
[root@centos6 /]# tr 'a-z' 'A-Z' < issue         #文件重定向到命令
  • 终止词
    终止词单独一行,有且仅有终止词才生效。
[root@centos6 app]# bc << end
> 1+1+2+3+4*100
> end
407
[root@centos6 app]# cat <<end
> hello
> word end                                       #终止词无效
> end                                            #只要未输入终止符
#                                                 之前操作暂未写到文件中
hello
word end

传送门
Linux练习题-重定向与管道
Linux日常练习题
Linux学习笔记

相关文章

  • 标准I/O与管道

    1. Linux一共三种I/O设备: 标准输入---0(默认接收键盘)标准输出---1(默认输出到终端)标准错误-...

  • C语言编程学习——标准I/O(标准输入输出)

    标准I/O: 1.I/O(Input/Output)就是输入与输出的简称. 2.标准I/O是ANSI C标准(C库...

  • linux系统API,c,c++的标准I/O和文件I/O

    标准I/O:数据从内存与标准i/o设备(stdio,stdout,stderr)之间的数据流动 文件I/O:文件的...

  • C语言学习12.文件和IO

    I/O:input/output,输入输出。分三类:标准I/O,串I/O,文件I/O 标准I/O: 标准输入:从标...

  • 5-标准I/O和管道

    本章内容 ◆ 三种I/O设备◆ 把I/O重定向至文件◆ 使用管道 标准输入和输出 把输出和错误重新定向到文件 把输...

  • 系统I/O 与 I/O标准库

    IO是一个比较大的概念,它所涉及到的内容也比较多,比较繁杂,下面就从文件的IO开始说起,因为unix下,一切皆文件...

  • 系统网络50问

    系统网络50问1、标准文件IO与文件IO的区别?答:标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是...

  • 系统网络50问

    系统网络50问1、标准文件IO与文件IO的区别?答:标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是...

  • I/O重定向与 管道

    A、三种I/O设备 标准输入(stdin) -0 #默认接受来自键盘的输入 标准输出(stdout) -1 ...

  • 标准输入输出(I/O)和管道

    一、标准的输入输出: (1)linux给程序提供三种I/O设备:1、标准输入 STDIN:-0 默认...

网友评论

    本文标题:标准I/O与管道

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