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
相關文章
相關標籤/搜索