重定向

标准输入和输出

  • 程序:指令+ 数据
  • 读入数据:Input
  • 输出数据:Output
  • 打开的文件都有一个fd: file descriptor (文件描述符)
Linux 给程序提供三种I/O
  • 标准输入(STDIN )-0 默认接受来自键盘的输入

  • 标准输出(STDOUT )-1 默认输出到终端窗口

  • [root@centos6 ~]# ls

anaconda-ks.cfg  Desktop  Documents  Downloads  install.log  install.log.syslog  kk  ll  Music  Pictures  Public  Templates  Videos
  • 标准错误(STDERR )-2 默认输出到终端窗口
[root@centos6 Packages]# lk
-bash: lk: command not found
  • I/O重定向:改变默认位置
[root@centos6 ~]# ls > /dev/pts/1   重定向另一个窗口

[root@centos6 ~]# tty
/dev/pts/1
[root@centos6 ~]# anaconda-ks.cfg  Desktop  Documents  Downloads  ```
install.log  install.log.syslog  kk  ll  Music  Pictures  Public  Templates  Videos  输出值tty的窗口

与>> 的区别

  • > 1.txt 表示如果1.txt存在就情况里面所有数据如果文件不存在就创建1.txt文件
  • >>2.txt 表示如果文件2.txt 文件存在追加一个空格,如果不存在就创建
[root@centos6 ~]# ls > 1.txt   ">" 标准重定向输出
[root@centos6 ~]# cat 1.txt 

1.txt
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
kk
ll
Music
Pictures
Public
Templates
Videos

[root@centos6 ~]# hostname > 1.txt  ">"  它会覆盖之前文件中的内容
[root@centos6 ~]# cat 1.txt 

centos6.chenxi.com

[root@centos6 ~]# ls >> 1.txt  >>与1>> 结果是一致的都是标准输出重定向追加的意思,不覆盖原文件的内容只是在后面追加
[root@centos6 ~]# cat 1.txt 

centos6.chenxi.com
1.txt
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
kk
ll
Music
Pictures
Public
Templates
Videos

- [root@centos6 ~]# 
- 标准错误重定向

[root@centos6 ~]# ls /d 2> h.txt 2表示标准错误 2> 标准错误重定向 2>> 标准错误重定向追加
[root@centos6 ~]#
[root@centos6 ~]# cat h.txt
ls: cannot access /d: No such file or directory

- 标准混合重定向

[root@centos6 ~]# ls /etc/ /tr > 3.txt 2>&1 与 [root@centos6 ~]# ls /etc/ /tr &> 4.txt 这个命令也可追加 &>> 和[root@centos6 ~]# ls /etc/ /tr >& 4.txt 功能都是一样的
[root@centos6 ~]# cat 3.txt
ls: cannot access /tr: No such file or directory
/etc/:
abrt
acpi
adjtime
akonadi
aliases
aliases.db

- set -C:  禁止将内容覆盖 已有文件, 但可追加;
- \>| file 强制覆盖  用法 >| 文件名
- set +C: 允许覆盖
- 标准输出与标准错误分别重定向至不同文件

[root@centos6 ~]# ls /etc/ /tr > 4.txt 2> 5.txt

- 同时多条命令执行结果重定向至一个文件

[root@centos6 ~]# (ls;pwd)>7.txt # 应加()把多条命令括起来,它会先执行()里的命令,如果不括起来的话它会先执行pwd>7.txt文件里


- 把标准输出改变成标准错误

[root@centos6 ~]# ( ls 1>&2 ) > 8.txt     命令表示ls 1>&2 命令 如果结果是标准输出 它会输出重定向至8.txt 如果标准错误就会在屏幕上打印 8.txt 系统也会创建出来 应为因为>号前面是空 ;空> 也会创建文件

1.txt 3.txt 4.txt 5.txt 7.txt 8.txt anaconda-ks.cfg Desktop Documents Downloads h.txt install.log install.log.syslog kk ll Music Pictures Public Templates Videos


- [root@centos6 ~]# ( ls 1>&2 ) 2> 8.txt 屏幕不会有输出了因为标准错误已被重定向至 8.txt 文件中 
- cat命令的标准输出重定向
- [root@centos6 ~]# cat cat #命令需要标准输入也需要标准输出

rrr
rrr
fyl,
fyl,
\hftf
\hftf\


- [root@centos6 ~] # cat >c.txt    标准输出重定向单行重定向

yyy
ttt
ttt
^C

[root@centos6 ~]# cat c.txt 

yyy
ttt
ttt

- [root@centos6 ~]# cat >t <<ed  多行重定向到t文件  << 指定结束符 

dd
ff
ff
eed
cd'
ff
ed 结束符前面不能加空格

- [root@centos6 ~]# cat t

dd
ff
ff
eed
cd'
ff

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、文件描述符 打开的文件都有一个fd: file descriptor (文件描述符),在/proc/$$/fd...
    张大志的博客阅读 940评论 0 0
  • 一、I/O重定向基本概念 I/O重定向有三种定义打开文件:stdin (the keyboard), stdout...
    Joah_Li阅读 1,503评论 0 2
  • tr,tee 程序的IO 简单的说程序由指令(命令)和数据(操作对象)组成,在linux上一切皆文件,程序操作对象...
    香吉矢阅读 724评论 0 1
  • 第二十七章 [原文] 善行,无辙迹;善言,无瑕谪;善数,不用筹策;善闭,无关楗而不可开;善结,无绳约而不可解。是以...
    道形图阅读 439评论 0 9
  • 连续测了2天晨尿,每次都是刺眼的白板。成功越来越渺茫,失败越来越明显。忍不住,上午给老公发了微信,告诉他连续两天白...
    真冉阅读 350评论 0 0