Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining whether STDERR is going to terminal

I have a suite of Java programs which are used as command-line tools on our Linux servers. Most of them use a class that prints a progress bar on STDERR, similar to Perl's Term::ProgressBar.

I'd like to have the progress bar shown whenever STDERR is going to the terminal and automatically disable itself when STDERR is redirected so that there aren't all sorts of progress bar pieces in the redirected data.

Checking System.console() == null was my first thought, but redirecting STDOUT is enough to make this true, even if STDERR is still going to the terminal. Is there anything I can check that is specific to STDERR? A solution that is Linux-specific or that uses native APIs would be ok for my needs.

like image 678
Brad Mace Avatar asked Nov 12 '22 23:11

Brad Mace


1 Answers

I think what you're looking for is isatty(3), in unistd.h. There's no way to tell whether a file handle has been redirected, period, but that'll tell you whether it's still interactive. See the source for the tty command in GNU coreutils.

like image 183
chrylis -cautiouslyoptimistic- Avatar answered Nov 14 '22 21:11

chrylis -cautiouslyoptimistic-