/dev/null的用途__shell

來源:互聯網
上載者:User
/dev/null ,從名稱上可以很顯然看出是一個空檔案(寫入到/dev/null時全部丟失,讀取/dev/null時自己返回EOF),那麼你會很疑惑,他到底有什麼用途呢,請看下文聽我講解,可能你在很多指令碼裡看過 /dev/null,具體總結下幾種常見用途.
一、禁止標準輸出. eg:      cat $filename >/dev/null       # 檔案內容丟失,不會輸出到標準輸出,.
二、禁止標準錯誤 eg:      rm $badname 2>/dev/null       #刪除檔案錯誤時,不會再有提示到終端,都丟到/dev/null裡去了
三、禁止標準輸出和標準錯誤的輸出. eg1:      cat $filename 2>/dev/null >/dev/null      # 如果"$filename"不存在,將不會有任何錯誤資訊提示.     # 如果"$filename"存在, 檔案的內容不會列印到標準輸出.      # 因此, 上面的代碼根本不會輸出任何資訊.      # 當只想測試命令的退出碼而不想有任何輸出時非常有用。   eg2:     #-----------測試命令的退出 begin ----------------------#       ls dddd 2>/dev/null 8      echo $?    //輸出命令結束代碼:0為命令正常執行,1-255為有出錯。       #-----------測試命令的退出 end-----------#         cat $filename &>/dev/null 
四、清除記錄檔內容 eg:     cat /dev/null > /var/log/messages      #  : > /var/log/messages   有同樣的效果, 但不會產生新的進程.(因為:是內建的)        cat /dev/null > /var/log/wtmp
五、 隱藏cookie而不再使用 eg:     if [ -f ~/.netscape/cookies ]  # 如果存在則刪除.       then         rm -f ~/.netscape/cookies       fi     ln -s /dev/null ~/.netscape/cookies      # 現在所有的cookies都會丟棄而不會儲存在磁碟上了.

聯繫我們

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