Linux-IO重定向與管道

1. 輸入與輸出

  • 標準輸入 STDIN
    文件描述符:0,默認:鍵盤輸入
  • 標準輸出 STDOUT
    文件描述符:1,默認:屏幕輸出
  • 錯誤輸出 STDERR
    文件描述符:2,默認:屏幕輸出

2. 標準輸出重定向

  • 覆蓋輸出 >shell

  • 追加輸出 >>centos

注意:shell的內嵌命令set能夠設置是否容許輸出重定向至已存在的文件bash

set -C:禁止輸出重定向至已存在的文件
set +C:容許輸出重定向至已存在的文件

示例:標準輸出重定向到文件(實際動做:先建立文件,再向其中寫入標準輸出內容)code

[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, sh
[root@VM_41_201_centos ~]# ls -m > ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
[root@VM_41_201_centos ~]# ls -m >> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.txt, sh
anaconda-ks.cfg, ls.txt, sh
[root@VM_41_201_centos ~]#

3. 錯誤輸出重定向

  • 覆蓋輸出 2>字符串

  • 追加輸出 2>>terminal

示例:錯誤輸出重定向到文件input

[root@VM_41_201_centos ~]# lss 2> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# lss 2>> ls.error
[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]#

4. 合併標準輸出與錯誤輸出重定向

  • 覆蓋輸出 &>
COMMAND &> 文件  
COMMAND > 文件 2>&1    #不推薦這種形式,難記,很差理解
COMMAND > 文件A 2> 文件B
  • 追加輸出 &>>
COMMAND &>> 文件  
COMMAND >> 文件 2>&1    #不推薦這種形式,難記,很差理解
COMMAND > 文件A 2> 文件B

示例:合併標準輸出與錯誤輸出重定向it

# 標準輸出
[root@VM_41_201_centos ~]# ls -m
anaconda-ks.cfg, ls.error, ls.txt, sh

# 標準輸出重定向到文件
[root@VM_41_201_centos ~]# ls -m &> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh

# 錯誤輸出重定向到文件(追加)
[root@VM_41_201_centos ~]# lss &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
-bash: lss: 未找到命令

# 合併標準輸出與錯誤輸出(標準輸出,覆蓋)
[root@VM_41_201_centos ~]# ls -m > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
anaconda-ks.cfg, ls.error, ls.txt, sh

# 合併標準輸出與錯誤輸出(錯誤輸出,覆蓋)
[root@VM_41_201_centos ~]# lss > ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令

# 合併標準輸出與錯誤輸出(錯誤輸出,追加)
[root@VM_41_201_centos ~]# lsss >> ls.txt 2>&1
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令

# 合併標準輸出與錯誤輸出(錯誤輸出,追加 ,另外一種方式)
[root@VM_41_201_centos ~]# lsls &>> ls.txt
[root@VM_41_201_centos ~]# cat ls.txt
-bash: lss: 未找到命令
-bash: lsss: 未找到命令
-bash: lsls: 未找到命令
[root@VM_41_201_centos ~]#

#使用COMMAND > 文件A 2> 文件B、COMMAND >> 文件A 2>> 文件B的形式
[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg  ls.error  ls.txt  sh  tee.txt
[root@VM_41_201_centos ~]# ls -m > A 2> B
[root@VM_41_201_centos ~]# ls -m
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat A
A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt
[root@VM_41_201_centos ~]# cat B
[root@VM_41_201_centos ~]# sl -m > A 2> B
[root@VM_41_201_centos ~]# cat A
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
[root@VM_41_201_centos ~]# lss -m >> A 2>> B
[root@VM_41_201_centos ~]# cat B
-bash: sl: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]#

5. 輸入重定向

輸入重定向相比較而言,就比較簡單了,並且用得相對較少file

通常用法:將文件做爲輸入重定向到命令程序

命令 < 文件

示例:將文件輸入重定向至命令wc統計文件行數:

[root@VM_41_201_centos ~]# wc -l < anaconda-ks.cfg
148
[root@VM_41_201_centos ~]#

6. 管道

管道用於鏈接多個命令(程序),將前一個命令的結果重定向,做爲後一個命令的輸入

COMMAND1 | COMMAND2 | COMMAND3 | ...

示例:將文件內容經過管道重定向到命令(其效果相似輸入重定向)

[root@VM_41_201_centos ~]# cat anaconda-ks.cfg | wc -l
148
[root@VM_41_201_centos ~]#

7. 相關命令

如下幾個命令常常與IO重定向(> >> 2> 2>> &> &>> )管道(|)結合使用

  • tee
    read from standard input and write to standard output and files

tee命令比較特殊:從標準輸入讀取、寫入標準輸出、寫入文件(同時幹了3件事,一箭三雕),IO重定向與tee更配哦。

示例:tee命令(執行結果既輸出到了指定文件,又輸出到了terminal)

[root@VM_41_201_centos ~]# ls
anaconda-ks.cfg  ls.error  ls.txt  sh
[root@VM_41_201_centos ~]# ls -m | tee tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]# cat tee.txt
anaconda-ks.cfg, ls.error, ls.txt, sh
[root@VM_41_201_centos ~]#
  • tr - translate or delete characters

tr命令用於替換及刪除字符

SYNOPSIS

tr [OPTION]... SET1 [SET2]

OPTIONS

-c, -C, --complement
    use the complement of SET1

-d, --delete
    delete characters in SET1, do not translate

-s, --squeeze-repeats
    replace each input sequence of  a  repeated  character  that  is listed in SET1 with a single occurrence of that character

-t, --truncate-set1
    first truncate SET1 to length of SET2

示例

[root@VM_41_201_centos ~]# cat ls.error
-bash: lss: 未找到命令
-bash: lss: 未找到命令
[root@VM_41_201_centos ~]# cat ls.error | tr b B
-Bash: lss: 未找到命令
-Bash: lss: 未找到命令
[root@VM_41_201_centos ~]#
  • 輸入重定向分界符:<<

容許用戶一直輸入,直到輸入的內容匹配<<指定的字符串

# EOF能夠用其它字符代替,習慣用EOF,end of file
命令 << EOF

示例:

[root@VM_41_201_centos ~]#
[root@VM_41_201_centos ~]# cat << EOF
> 1.a
> 2.b
> 3.c
> EOF
1.a
2.b
3.c
[root@VM_41_201_centos ~]#
相關文章
相關標籤/搜索