IO重定向

程序:指令+数据

读入数据:input
输出数据:outputcentos

打开的文件都有一个fd:file descriptor 文件描述符
Linux给程序提供三种I/O设备bash

  • 标准输入(STDIN) -0 默认接受来自终端窗口的输入
  • 标准输出(STDOUT)-1 默认输出到终端窗口
  • 标准错误(STDERR)-2 默认输出到终端窗口
[root@centos7 proc]# echo $$
2199
[root@centos7 proc]# ll /proc/$$/fd
总用量 0
lrwx------. 1 root root 64 10月 19 20:09 0 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:09 1 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:09 2 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:22 255 -> /dev/pts/0
[root@centos7 proc]#
[root@centos7 proc]# ll /proc/self/fd
总用量 0
lrwx------. 1 root root 64 10月 19 20:25 0 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:25 1 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:25 2 -> /dev/pts/0
lr-x------. 1 root root 64 10月 19 20:25 3 -> /proc/2222/fd

I/O重定向redirect

格式:命令 操做符号 文件名ide

支持的操做符号
1> or >:重定向标准输出
2>:重定向标准错误centos7

[root@centos7 ~]# xxx 2> /data/f1.log
[root@centos7 ~]# cat /data/f1.log
-bash: xxx: 未找到命令
[root@centos7 ~]# rm /data/f1.log 2> /data/all.log
y
[root@centos7 ~]# cat /data/all.log 
rm:是否删除普通文件 "/data/f1.log"?[root@centos7 ~]#

&>:同时重定向标准输出和错误code

[root@centos7 ~]# ls /data/ /rr &> /data/all.log 
[root@centos7 ~]# cat /data/all.log
ls: 没法访问/rr: 没有那个文件或目录
/data/:
all.log

以上若是文件已经存在,文件内容会被覆盖
set -c 禁止将内容覆盖已有文件,但可追加;强制覆盖 &|
set +c容许覆盖ip

追加

  • ’>> 1‘
  • ’2>>‘
  • ’&>>‘

标准输入重定向

<input

[root@centos7 ~]# cat bc.log 
2+3
[root@centos7 ~]# bc < bc.log 
5
[root@centos7 ~]# seq -s+ 1 10 > bc.log
[root@centos7 ~]# bc < bc.log 
55
相关文章
相关标签/搜索