Pipe stderr only if a terminal application crashes The 2019 Stack Overflow Developer Survey Results Are InRedirect stderr from an already running scriptRedirect stdout and stderr, with timestamps on stderr onlyHow to strip color codes out of stdout and pipe to file and stdoutHow to capture ordered STDOUT/STDERR and add timestamp/prefixes?A way to distinguish between interleaved output from two background processesBash interactive mode on redirectWhy is /dev/stderr invalid when redirecting to a pipe in cygwin?Send stderr to a different receiver in pipeDetermine if process was launched directly from a terminal window or programmatically as a child processUsing pipe STDOUT as a variable?

What tool would a Roman-age civilization have for the breaking of silver and other metals into dust?

Does coating your armor in silver add any effects?

What is the accessibility of a package's `Private` context variables?

Reference request: Oldest number theory books with (unsolved) exercises?

Why isn't airport relocation done gradually?

A poker game description that does not feel gimmicky

Button changing it's text & action. Good or terrible?

Right tool to dig six foot holes?

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

How technical should a Scrum Master be to effectively remove impediments?

Looking for Correct Greek Translation for Heraclitus

Output the Arecibo Message

Did Section 31 appear in Star Trek: The Next Generation?

Can someone be penalized for an "unlawful" act if no penalty is specified?

What is the most effective way of iterating a std::vector and why?

Why is the Constellation's nose gear so long?

Can one be advised by a professor who is very far away?

Why do UK politicians seemingly ignore opinion polls on Brexit?

Is there a symbol for a right arrow with a square in the middle?

Multiply Two Integer Polynomials

FPGA - DIY Programming

Why can Shazam fly?

Is an up-to-date browser secure on an out-of-date OS?

What is the motivation for a law requiring 2 parties to consent for recording a conversation



Pipe stderr only if a terminal application crashes



The 2019 Stack Overflow Developer Survey Results Are InRedirect stderr from an already running scriptRedirect stdout and stderr, with timestamps on stderr onlyHow to strip color codes out of stdout and pipe to file and stdoutHow to capture ordered STDOUT/STDERR and add timestamp/prefixes?A way to distinguish between interleaved output from two background processesBash interactive mode on redirectWhy is /dev/stderr invalid when redirecting to a pipe in cygwin?Send stderr to a different receiver in pipeDetermine if process was launched directly from a terminal window or programmatically as a child processUsing pipe STDOUT as a variable?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a program which interacts with the user on the terminal.



But if it crashes I'd like to pipe the stderr through another tool to tidy / prettyprint it.



Is there a way to do this?



As far as I can see, the ways of redirecting the stderr all involve doing something with stdout, and that hides / disrupts the interactive terminal.










share|improve this question






















  • Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

    – Kusalananda
    yesterday












  • What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

    – interstar
    yesterday






  • 1





    Are you able to get the program to do logging to a file, i.e. from the program itself?

    – Kusalananda
    yesterday











  • Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

    – interstar
    yesterday

















1















I have a program which interacts with the user on the terminal.



But if it crashes I'd like to pipe the stderr through another tool to tidy / prettyprint it.



Is there a way to do this?



As far as I can see, the ways of redirecting the stderr all involve doing something with stdout, and that hides / disrupts the interactive terminal.










share|improve this question






















  • Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

    – Kusalananda
    yesterday












  • What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

    – interstar
    yesterday






  • 1





    Are you able to get the program to do logging to a file, i.e. from the program itself?

    – Kusalananda
    yesterday











  • Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

    – interstar
    yesterday













1












1








1


0






I have a program which interacts with the user on the terminal.



But if it crashes I'd like to pipe the stderr through another tool to tidy / prettyprint it.



Is there a way to do this?



As far as I can see, the ways of redirecting the stderr all involve doing something with stdout, and that hides / disrupts the interactive terminal.










share|improve this question














I have a program which interacts with the user on the terminal.



But if it crashes I'd like to pipe the stderr through another tool to tidy / prettyprint it.



Is there a way to do this?



As far as I can see, the ways of redirecting the stderr all involve doing something with stdout, and that hides / disrupts the interactive terminal.







bash pipe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









interstarinterstar

3621821




3621821












  • Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

    – Kusalananda
    yesterday












  • What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

    – interstar
    yesterday






  • 1





    Are you able to get the program to do logging to a file, i.e. from the program itself?

    – Kusalananda
    yesterday











  • Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

    – interstar
    yesterday

















  • Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

    – Kusalananda
    yesterday












  • What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

    – interstar
    yesterday






  • 1





    Are you able to get the program to do logging to a file, i.e. from the program itself?

    – Kusalananda
    yesterday











  • Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

    – interstar
    yesterday
















Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

– Kusalananda
yesterday






Are you asking about the pretty printing, or about how to arrange for that other tool to get the error output, or both?

– Kusalananda
yesterday














What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

– interstar
yesterday





What I really want right now is the pretty-printing what comes out on stderr if the interactive program crashes. I thought this would be the general case that would also solve that. But I'm open to other solutions

– interstar
yesterday




1




1





Are you able to get the program to do logging to a file, i.e. from the program itself?

– Kusalananda
yesterday





Are you able to get the program to do logging to a file, i.e. from the program itself?

– Kusalananda
yesterday













Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

– interstar
yesterday





Not simply. If this can't be done, then it can't be done, and I'll have to look for another solution. Exactly explicitly trying to intercept errors etc. I was just hoping there was some command-line / pipeline magic that could solve it.

– interstar
yesterday










1 Answer
1






active

oldest

votes


















0














You might have to wrap the program in a small bash script, but it's doable. You can redirect stderr to a file while leaving stdout to display on the screen:



#!/usr/bin/env bash

# Run my program, with stderr saved to a file
my_program 2> /tmp/stderr_output.txt

# If the program returned with anything but success, print stderr
if [ ! $! ]; then
cat /tmp/stderr_output.txt
fi


Instead of just using the cat utility, you could use your formatting utility to output the error text.






share|improve this answer


















  • 4





    $! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

    – Kusalananda
    yesterday












  • Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

    – interstar
    yesterday






  • 1





    @interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

    – Kusalananda
    yesterday











  • So basically, can't be done then?

    – interstar
    yesterday











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511351%2fpipe-stderr-only-if-a-terminal-application-crashes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You might have to wrap the program in a small bash script, but it's doable. You can redirect stderr to a file while leaving stdout to display on the screen:



#!/usr/bin/env bash

# Run my program, with stderr saved to a file
my_program 2> /tmp/stderr_output.txt

# If the program returned with anything but success, print stderr
if [ ! $! ]; then
cat /tmp/stderr_output.txt
fi


Instead of just using the cat utility, you could use your formatting utility to output the error text.






share|improve this answer


















  • 4





    $! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

    – Kusalananda
    yesterday












  • Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

    – interstar
    yesterday






  • 1





    @interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

    – Kusalananda
    yesterday











  • So basically, can't be done then?

    – interstar
    yesterday















0














You might have to wrap the program in a small bash script, but it's doable. You can redirect stderr to a file while leaving stdout to display on the screen:



#!/usr/bin/env bash

# Run my program, with stderr saved to a file
my_program 2> /tmp/stderr_output.txt

# If the program returned with anything but success, print stderr
if [ ! $! ]; then
cat /tmp/stderr_output.txt
fi


Instead of just using the cat utility, you could use your formatting utility to output the error text.






share|improve this answer


















  • 4





    $! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

    – Kusalananda
    yesterday












  • Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

    – interstar
    yesterday






  • 1





    @interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

    – Kusalananda
    yesterday











  • So basically, can't be done then?

    – interstar
    yesterday













0












0








0







You might have to wrap the program in a small bash script, but it's doable. You can redirect stderr to a file while leaving stdout to display on the screen:



#!/usr/bin/env bash

# Run my program, with stderr saved to a file
my_program 2> /tmp/stderr_output.txt

# If the program returned with anything but success, print stderr
if [ ! $! ]; then
cat /tmp/stderr_output.txt
fi


Instead of just using the cat utility, you could use your formatting utility to output the error text.






share|improve this answer













You might have to wrap the program in a small bash script, but it's doable. You can redirect stderr to a file while leaving stdout to display on the screen:



#!/usr/bin/env bash

# Run my program, with stderr saved to a file
my_program 2> /tmp/stderr_output.txt

# If the program returned with anything but success, print stderr
if [ ! $! ]; then
cat /tmp/stderr_output.txt
fi


Instead of just using the cat utility, you could use your formatting utility to output the error text.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Jacob HumeJacob Hume

1785




1785







  • 4





    $! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

    – Kusalananda
    yesterday












  • Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

    – interstar
    yesterday






  • 1





    @interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

    – Kusalananda
    yesterday











  • So basically, can't be done then?

    – interstar
    yesterday












  • 4





    $! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

    – Kusalananda
    yesterday












  • Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

    – interstar
    yesterday






  • 1





    @interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

    – Kusalananda
    yesterday











  • So basically, can't be done then?

    – interstar
    yesterday







4




4





$! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

– Kusalananda
yesterday






$! is the PID of the most recent background task, The return status is in $?. You can't test for a non-zero integer with [ ! $? ] (this tests for an empty string, as with [ -z $? ]). It would be better to just use if ! my_program 2>file; then ...; fi.

– Kusalananda
yesterday














Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

– interstar
yesterday





Doesn't seem to work. If I do my_program 2> /tmp/errors.txt that still blocks my interaction on the terminal

– interstar
yesterday




1




1





@interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

– Kusalananda
yesterday





@interstar Prompts etc. (interactive dialog with the user) happens on standard error. Redirecting it to a file would put all of that into the file.

– Kusalananda
yesterday













So basically, can't be done then?

– interstar
yesterday





So basically, can't be done then?

– interstar
yesterday

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511351%2fpipe-stderr-only-if-a-terminal-application-crashes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







-bash, pipe

Popular posts from this blog

Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

Nikolai Prilezhaev Bibliography References External links Navigation menuEarly Russian Organic Chemists and Their Legacy092774english translationRussian Biography

How to link a C library to an Assembly library on Mac with clangHow do you set, clear, and toggle a single bit?Find (and kill) process locking port 3000 on MacWho is listening on a given TCP port on Mac OS X?How to start PostgreSQL server on Mac OS X?Compile assembler in nasm on mac osHow do I install pip on macOS or OS X?AFNetworking 2.0 “_NSURLSessionTransferSizeUnknown” linking error on Mac OS X 10.8C++ code for testing the Collatz conjecture faster than hand-written assembly - why?How to link a NASM code and GCC in Mac OS X?How to run x86 .asm on macOS Sierra