如果你是一個忙碌的人,那麼你最不需要的就是被大量“無用”的通知所困擾,那麼你如何讓事情平靜下來呢?今天的超級使用者問答有一些很好的答案,可以幫助讀者降低輸出量。
今天的問答環節是由SuperUser提供的,SuperUser是Stack Exchange的一個分支,是一個由社群驅動的問答網站分組。
超級使用者讀者Xster想知道如何在後臺執行沒有輸出的命令,除非出現錯誤:
How do you suppress a command’s output, but show it if the command’s exit codes an error?
如何讓命令在後臺執行而不輸出,除非有錯誤?
超級使用者貢獻者Bob和Maximillian Laumeister為我們提供了答案。首先,鮑勃:
Unfortunately, the assumption that stderr is only used for error output is not always correct. Rather, stderr is often used for any and all interactive output and diagnostics (i.e. output intended for the user to read in an interactive prompt).(1) wget and dd are well-known examples.
Some commands will provide a flag (i.e. -quiet or -silent) to suppress non-error output. Read their man pages to see if one exists.
Another convention that holds more often is the exit code, a program returns an exit code when it exits. Typically(2), an exit code of 0 indicates success, and any other exit code indicates an error.
With bash, you can get the exit code of the last command from the $? variable. In fish, use the $status variable. You can pipe stderr to a temporary file and only print it if an error occurs. For example (fish):
You can also use some shortcuts if you are not chaining commands:
Or:
You can also pipe stdout to the same buffer by using 2>&1 >/tmp/outputbuffer.
(Note: I do not actually know fish, so I am adapting the concept to what I can find in its documentation. The syntax might be slightly wrong. Also, you can use mktemp to generate a unique temporary file. Run it and record the file name in a variable.)
If you need to run the whole thing in the background of a shell that you are also using interactively at the same time, then you are better off writing a script to handle the output-hiding and running that script in the background with the standard techniques (fish). Heck, you can put something like the following function in ~/.config/fish/config.fish:
Call with run-silent somecommand & (where the trailing & causes it to run in the background)
Note that this will swallow the original exit code, and will dump both stdout and stderr in the event of a failure. You can customise it as necessary.
(1) There is no guarantee that error output will not appear on stdout, some programs will dump all output there!
(2) Unfortunately, this is still not always the case. The exit code is completely controlled by the program and some will indicate some success conditi*** with non-zero exits. Again, check the manual.
接著是Maximillian Laumeister的回答:
Unix utilities send general messages to stdout, and error messages to stderr, so if we only want to see error messages, then it will be sufficient to suppress stdout so that only stderr gets output to the c***ole.
The way to do this (in both bash and fish) is to append >/dev/null to the command. This pipes stdout into nothingness, but stderr (with your error messages) still comes through to the c***ole.
So for instance:
The command echo 1 >/dev/null prints nothing, because the normal stdout output is suppressed, and nothing was written to stderr.
The command man doesnotexist >/dev/null prints an error message, because man writes its error message to stderr.
有什麼要補充的解釋嗎?在評論中發出聲音。想從其他精通技術的Stack Exchange使用者那裡瞭解更多答案嗎?在這裡檢視完整的討論主題。
...dows10的現代應用程式可以在你電腦的後臺執行。下面介紹如何防止它們這樣做。 ...
androidoreo現在可用於部分裝置,並帶來了一些增量但明顯的變化。其中之一是一個新的永續性通知,它可以讓你知道應用程式何時在後臺執行。 ...
... 我們已經寫了關於如何使用CHKDSK的文章,所以如果你想開始的話就跳過去吧。這篇文章是關於Windows8和Windows10中的CHKDSK工具如何比Windows7中的CHKDSK工具有了巨大的改進。 ...
...jave是最後一個支援32位應用程式的版本。我們已經介紹瞭如何在升級之前檢查Mac上的32位應用程式。 ...
... 標題1:你的**壞了?下面介紹如何在windows10和Android上修復它 ...
... 在這裡,您將瞭解什麼是crontab,如何新增自動化作業,以及一些實際示例。 ...
...執行。這意味著Edge將繼續使用您計算機的資源。下面是如何停止後臺程序。 什麼是後臺應用程式(background apps)? 預設情況下,Microsoft Edge允許後臺應用程式和擴充套件在計算機的後臺執行。這使得需要全天候執行的擴充套件和...
...只想在系統有空閒資源時執行程序,可以使用批處理。 如何安排linux作業 cron守護程序維護它在特定時間執行的作業列表。這些任務和程式按預定時間在後臺執行。這為您安排需要重複的任務提供了極大的靈活性。無論您需要...