重定向

作者: 尘曦的雨 | 来源:发表于2017-06-26 22:53 被阅读3次

标准输入和输出

  • 程序:指令+ 数据
  • 读入数据: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

相关文章

  • 第07章重定向管道

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

  • shell 笔记 Day1

    重定向: (覆盖重定向), >>(追加重定向) , 2>(重定向错误信息) , &>(错误正确都重定...

  • 《Linux就该这么学 》笔记(六)| 管道符、重定向和环境变量

    1. 重定向 重定向技术的 5 种模式 标准覆盖输出重定向 标准追加输出重定向 错误覆盖输出重定向 错误追加输出重...

  • 永久性重定向和302临时性重定向

    什么是重定向? 所谓重定向就是将网页自动转向重定向,即:301永久性重定向和302临时性重定向。实施301后,新网...

  • 2019-06-13 重定向301和302

    什么是重定向? 所谓重定向就是将网页自动转向重定向,即:301永久性重定向和302临时性重定向。实施301后,新网...

  • 重定向

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

  • Linux重定向day13

    1.重定向概述2.重定向的输出输入3.进程管道技术 一、重定向概述 什么是重定向:Linux重定向是指修改原来默认...

  • Linux高级

    一.重定向命令 学习目标 能够使用重定向命令将终端显示内容重定向到文件 1. 重定向命令的介绍 重定向也称为输出重...

  • vue-router -其他

    一、 重定向 重定向也是通过 routes 配置来完成,下面例子是从 /me重定向到/home`: 重定向的目标也...

  • uos 输入输出与重定向

    1、实验-输出重定向 2、实验-错误重定向 3、实验-双重输出重定向 4、实验-输入重定向 5、实验-管道 6、实...

网友评论

    本文标题:重定向

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