簡體   English   中英

為什么在終端中沒有任何輸出的情況下我不能在Linux中執行某些命令?

[英]Why can't I execute some command in Linux without any output in the terminal?

我試圖在終端中執行“ rmdir *”。 由於有些不是當前文件夾下的目錄,因此將顯示錯誤消息,提示某些文件不在目錄中。 我的目標是無提示地靜默執行命令。

我嘗試了以下方法:

rmdir * > /dev/null
rmdir * > file.txt
rmdir * |grep "noexist"  (some non-existing characters)
rmdir * 2>&1 > /dev/null
rmdir * 2>&1 > file.txt
rmdir * 2>&1 |grep "noexist"

只有最后一個命令有效! 我感到有些困惑。 當最后一個命令起作用時,為什么第四個和第五個命令不起作用?

>重定向STDOUT。 2>重定向STDERR。 &1重定向到STDOUT。 | 將STDOUT轉移到STDIN以進行新過程。

你試過了嗎:

rmdir * > /dev/null 2> /dev/null

它將STDOUT和STDERR都重定向到空設備。

1) Doesn't redirect stderr using 2>...
2) See 1.
3) See 2.
4) Redirects stderr to stdout, and stdout is redirected to /dev/null, so you still see the redirected stderr on stdout.
5) See 4. The file to which stdout is redirected is just different.
6) This works because you redirected stderr to stdout. stdout is piped in to grep as grep's stdin.

真正起作用的是:

rmdir * >/dev/null 2>&1

為什么行得通? 因為第一個標准輸出被重定向到/ dev / null。 然后將stderr重定向到stdout,而該stdout已經重定向到/ dev / null。

除非目錄為空,否則您不能使用rmdir。確保執行此操作之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM