centos重新導向命令

來源:互聯網
上載者:User

centos重新導向命令

yw >outfile 2>&1$ cat outfile cat: oyyw: 沒有那個檔案或目錄

和

$ cat oyyw 2>&1 >outfilecat: oyyw: 沒有那個檔案或目錄
解惑

搜集了一部分資料以後,解答如下:

順序是很重要的!命令的執行是自左向右的,因此: * 對於第一個命令,shell首先讀到>outfile(等同於1>outfile),此時標準輸出重新導向到了檔案outfile,然後shell再讀到2>&1,此時標準錯誤輸出重新導向到了標準輸出,也就是重新導向到了檔案outfile,所以得到的結果是標準輸出和標準錯誤輸出都重新導向到了檔案outfile中

  • 對於第二個命令,shell首先讀到2>&1,此時標準錯誤輸出重新導向到了標準輸出(可能是控制台),然後shell再讀到>outfile(等同於1>outfile),此時標準輸出重新導向到了檔案outfile,得到的結果是標準錯誤輸出輸出到了控制台,標準輸出輸出到檔案outfile中

以下這個例子,應該可以很好解答上面的疑問了:

$ cat learn_redirect.sh #!/bin/bashfoo() {    echo "This is stdout";        # 輸出是標準輸出    echo "This is stderr" 1>&2;   # 輸出是標準錯誤輸出}foo >errlog 2>&1 >outfile
[pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ sh learn_redirect.sh [pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ cat outfile This is stdout[pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ cat errlog This is stderr
解釋如下

foo >errlog 2>&1 >outfile這一句命令,shell執行順序是從左向右。首先shell讀到>errlog,此時標準輸出重新導向到了檔案errlog,然後shell讀到2>&1,此時標準錯誤輸出重新導向到了標準輸出(等同於標準錯誤輸出重新導向到了檔案errlog),然後shell讀到>outfile,此時標準輸出重新導向到檔案outfile,因此,得到的結果是標準錯誤輸出輸出到檔案errlog中,標準輸出輸出到了檔案outfile中

參考資料

http://www.gnu.org/software/bash/manual/bash.html#Redirections http://mywiki.wooledge.org/BashFAQ/055 http://docstore.mik.ua/orelly/unix/upt/ch45_21.htm http://docstore.mik.ua/orelly/unix/upt/ch08_13.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.