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

Frič See also Navigation menuinternal link

Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox