Làm thế nào để bạn chạy một lệnh trong nền với không có đầu ra Trừ khi có một lỗi?

Mục lục:

Làm thế nào để bạn chạy một lệnh trong nền với không có đầu ra Trừ khi có một lỗi?
Làm thế nào để bạn chạy một lệnh trong nền với không có đầu ra Trừ khi có một lỗi?

Video: Làm thế nào để bạn chạy một lệnh trong nền với không có đầu ra Trừ khi có một lỗi?

Video: Làm thế nào để bạn chạy một lệnh trong nền với không có đầu ra Trừ khi có một lỗi?
Video: Lý do nên chọn máy Fax chuyên dụng | Máy Fax Laser đa chức năng Brother FAX-2840 - YouTube 2024, Tháng tư
Anonim
Nếu bạn là một người bận rộn, thì điều cuối cùng bạn cần là làm phiền với một lượng lớn thông báo 'vô dụng', vậy làm thế nào để bạn làm mọi thứ lắng xuống? Bài đăng Hỏi & Đáp của SuperUser hôm nay có một số câu trả lời tuyệt vời để giúp người đọc giảm bớt số lượng đầu ra.
Nếu bạn là một người bận rộn, thì điều cuối cùng bạn cần là làm phiền với một lượng lớn thông báo 'vô dụng', vậy làm thế nào để bạn làm mọi thứ lắng xuống? Bài đăng Hỏi & Đáp của SuperUser hôm nay có một số câu trả lời tuyệt vời để giúp người đọc giảm bớt số lượng đầu ra.

Phiên Hỏi & Đáp hôm nay đến với chúng tôi theo lịch sự của SuperUser - một phân khu của Stack Exchange, một nhóm các trang web Hỏi & Đáp do cộng đồng điều hành.

Câu hỏi

Trình đọc siêu người dùng Xster muốn biết cách chạy lệnh trong nền mà không có đầu ra trừ khi có lỗi:

How do you suppress a command’s output, but show it if the command’s exit codes an error?

Làm thế nào để bạn có được một lệnh để chạy trong nền mà không có đầu ra, trừ khi có một lỗi?

Câu trả lời

Người đóng góp của SuperUser Bob và Maximillian Laumeister có câu trả lời cho chúng tôi. Đầu tiên, Bob:

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:
You can also use some shortcuts if you are not chaining commands:
Or:
Or:
Image
Image

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:

Image
Image

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 conditions with non-zero exits. Again, check the manual.

Tiếp theo là câu trả lời từ 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 console.

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 console.

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.

Có cái gì để thêm vào lời giải thích? Tắt âm thanh trong các ý kiến. Bạn muốn đọc thêm câu trả lời từ những người dùng Stack Exchange có hiểu biết công nghệ khác? Xem toàn bộ chuỗi thảo luận tại đây.

Đề xuất: