
如何在 Linux 中隱藏 shell 應用程式的螢幕輸出 (printf)?
答案1
您可以重定向任何程式的輸出,使其不被看到。
$ program > /dev/null
這將重定向標準輸出 - 您仍然會看到任何錯誤
$ program &> /dev/null
這將重定向所有輸出,包括錯誤。
答案2
命令列上提供了三個 I/O 設備。
standard input - 0
standard output - 1
standard error - 2
若要將標準輸出(預設輸出)重定向到檔案(並覆寫該檔案),請使用
command > file.log
若要附加到 file.log,請使用兩個>
s
command >> file.log
若要將標準錯誤重定向到 file.log,請使用
command 2> file.log
並附加
command 2>> file.log
將輸出組合成一個流並將它們全部發送到一個地方
command > file.log 2>&1
這會將 2(標準錯誤)傳送到 1(標準輸出),並將標準輸出傳送到 file.log
請注意,也可以將標準輸入重新導向到需要標準輸入的命令
command << file.txt
欲了解更多詳情,請查看進階 Bash 腳本編寫指南。
答案3
隱藏標準輸出:
./command >/dev/null
隱藏標準和錯誤輸出:
./command >/dev/null 2>&1
隱藏標準輸出和錯誤輸出並釋放終端機(在後台運行命令):
./command >/dev/null 2>&1 &
答案4
為了Mac OS X v10.6(雪豹):
如果您需要透過檢查輸出/錯誤檔案描述符來隱藏輸出而不讓程式知道,您可以嘗試在 shell 中使用以下命令:
stty flusho; command ;stty -flusho
或者如果您只是想隱藏終端的輸入:
stty -echo; command ;stty echo
有關詳細信息,請參閱 stty(1) 手冊頁。
對於Linux,我所知道的是烏班圖10.04(Lucid Lynx)和一些 Debian/Arch Linux(下面評論 - 謝謝,hendry)沒有該flusho
設定(並且我在手冊頁中看不到任何其他合適的內容)。無論如何,該echo
設定在 Ubuntu 上有效。