Linux Shell指令碼編程--tee命令__html5

來源:互聯網
上載者:User
用途說明

在執行Linux命令時,我們可以把輸出重新導向到檔案中,比如 ls >a.txt,這時我們就不能看到輸出了,如果我們既想把輸出儲存到檔案中,又想在螢幕上看到輸出內容,就可以使用tee命令了。tee命令讀取標準輸入,把這些內容同時輸出到標準輸出和(多個)檔案中(read from standard input and write to standard output and files. Copy standard input to each FILE, and also to standard output. If a FILE is -, copy again to standard output.)。在info tee中說道:tee命令可以重新導向標準輸出到多個檔案(`tee': Redirect output to multiple files. The `tee' command copies standard input to standard output and also to any files given as arguments.  This is useful when you want not only to send some data down a pipe, but also to save a copy.)。要注意的是:在使用管道線時,前一個命令的標準錯誤輸出不會被tee讀取。

  常用參數

格式:tee

只輸出到標準輸出,因為沒有指定檔案。

 

格式:tee file

輸出到標準輸出的同時,儲存到檔案file中。如果檔案不存在,則建立;如果已經存在,則覆蓋之。(If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously
contained is overwritten unless the `-a' option is used.)

 

格式:tee -a file

輸出到標準輸出的同時,追加到檔案file中。如果檔案不存在,則建立;如果已經存在,就在末尾追加內容,而不是覆蓋。

 

格式:tee -

輸出到標準輸出兩次。(A FILE of `-' causes `tee' to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)

 

格式:tee file1 file2 -

輸出到標準輸出兩次,同時儲存到file1和file2中。

  使用樣本 樣本一 tee命令與重新導向的對比

[root@web ~]# seq 5 >1.txt
[root@web ~]# cat 1.txt
1
2
3
4
5
[root@web ~]# cat 1.txt >2.txt
[root@web ~]# cat 1.txt | tee 3.txt
1
2
3
4
5
[root@web ~]# cat 2.txt
1
2
3
4
5
[root@web ~]# cat 3.txt
1
2
3
4
5
[root@web ~]# cat 1.txt >>2.txt
[root@web ~]# cat 1.txt | tee -a 3.txt
1
2
3
4
5
[root@web ~]# cat 2.txt
1
2
3
4
5
1
2
3
4
5
[root@web ~]# cat 3.txt
1
2
3
4
5
1
2
3
4
5
[root@web ~]#

  樣本二 使用tee命令重複輸出字串

[root@web ~]# echo 12345 | tee
12345

[root@web ~]# echo 12345 | tee -
12345
12345
[root@web ~]# echo 12345 | tee - -
12345
12345
12345
[root@web ~]# echo 12345 | tee - - -
12345
12345
12345
12345
[root@web ~]# echo 12345 | tee - - - -
12345
12345
12345
12345
12345
[root@web ~]#

[root@web ~]# echo -n 12345 | tee

12345[root@web ~]# echo -n 12345 | tee -
1234512345[root@web ~]# echo -n 12345 | tee - -
123451234512345[root@web ~]# echo -n 12345 | tee - - -
12345123451234512345[root@web ~]# echo -n 12345 | tee - - - -
1234512345123451234512345[root@web ~]#

  樣本三 使用tee命令把標準錯誤輸出也儲存到檔案

[root@web ~]# ls "*"
ls: *: 沒有那個檔案或目錄
[root@web ~]# ls "*" | tee -
ls: *: 沒有那個檔案或目錄
[root@web ~]# ls "*" | tee ls.txt
ls: *: 沒有那個檔案或目錄
[root@web ~]# cat ls.txt
[root@web ~]# ls "*" 2>&1 | tee ls.txt
ls: *: 沒有那個檔案或目錄
[root@web ~]# cat ls.txt
ls: *: 沒有那個檔案或目錄

樣本四 列出常值內容,同時複製3份複本

列出文字檔slayers.story的內容,同時複製3份副本,檔案名稱分別為ss-copy1、ss-copy2、ss-copy3:
[root@web ~]#  cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.