问题描述
我有一套Java程序,在我们的Linux服务器上用作命令行工具。他们中的大多数使用在STDERR上打印进度条的类,类似于Perl的 Term :: ProgressBar
。
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
.
我想在STDERR进入终端时显示进度条,并在重定向STDERR时自动禁用自身,以便重定向数据中没有所有类型的进度条。
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.
检查 System.console()== null
是我的第一个想法,但重定向STDOUT足以使此为true
,即使STDERR仍然要去终端。有什么我可以检查特定于STDERR?特定于Linux或使用本机API的解决方案可以满足我的需求。
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.
推荐答案
我想你是什么在 unistd.h
中查找 isatty(3)
。没有办法判断文件句柄是否已被重定向,句点,但是它会告诉你它是否仍然是交互式的。请参阅GNU coreutils中 tty
命令的源代码。
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.
这篇关于确定STDERR是否要转到终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!