2024 Autor: Peter John Melton | [email protected]. Naposledy změněno: 2024-01-31 06:09
Dnešní zasedání Otázky a odpovědi se k nám přichází s laskavým svolením SuperUser - rozdělení Stack Exchange, komunitní skupiny webových stránek pro otázky a odpovědi.
Otázka
Čtečka SuperUser Xster chce vědět, jak spustit příkaz na pozadí bez výstupu, pokud se neobjeví chyba:
How do you suppress a command’s output, but show it if the command’s exit codes an error?
Jak dostanete příkaz k běhu na pozadí bez výstupu, pokud se neobjeví chyba?
Odpověď
Uživatelé SuperUser Bob a Maximillian Laumeister mají pro nás odpověď. První, 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 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 conditions with non-zero exits. Again, check the manual.
Následuje odpověď od Maximilliana Laumeistera:
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.
Musíte něco přidat k vysvětlení? Zní to v komentářích. Chcete se dozvědět více odpovědí od ostatních uživatelů technologie Stack Exchange? Podívejte se na celý diskusní příspěvek zde.
Doporučuje:
Jak odstranit pozadí pozadí pomocí aplikace PowerPoint 2010
Nedávno jsme vám ukázali spoustu způsobů, jak odstranit pozadí z fotografií pomocí Photoshopu a některých dalších nástrojů, ale víte, že můžete udělat totéž z aplikace PowerPoint 2010? Zde je postup.
Počkejte, dokud nedojde k odinstalování nebo změně aktuálního programu
Pokud obdržíte zprávu, počkejte, dokud nebude aktuální program dokončen odinstalací nebo změnou zprávy v počítači se systémem Windows.
Došlo k chybě. Zkuste se přihlásit k pozdější chybě systému Windows Store
Při pokusu o přihlášení do systému Windows Store se může stát, že došlo k chybě. Zkuste se přihlásit k pozdější zprávě. Zde je návod, jak to opravit.
Došlo k neočekávané chybě vstupu / výstupu, chyba 0xc00000e9 v systému Windows 10
Tento příspěvek ukazuje řešení problémů Problém s zařízením připojeným k počítači došlo k chybě IO Chyba Kód 0xc00000e9 v systému Windows.
Příkaz není rozpoznán jako interní nebo externí příkaz
Při pokusu spustit program nebo příkaz obdržíte zprávu, že není rozpoznán jako interní nebo externí příkaz, operabilní program nebo dávkový soubor problém, pak je třeba upravit Windows proměnné prostředí. Zde je návod, jak problém vyřešit.