zyndor
zyndor

Reputation: 1468

What is the way to separate command line output (processing from user interaction) on Unix?

I'm writing a console application in which user interaction might be necessary (prompt for keyboard input, cli arguments etc.), but I want to keep it separate from the result of the processing (which goes to cout, in a way that it can be piped to some other application).

How can I achieve this, if I can't just send all interaction with the user to cerr (not everything is an error)?

Upvotes: 2

Views: 163

Answers (3)

geekosaur
geekosaur

Reputation: 61439

/dev/tty is the usual way, but it's also possible on most Unix-like operating systems to read from cerr/stderr because the system usually opens the tty once as stdin and dup()s it onto stdout and stderr.

Upvotes: 3

AProgrammer
AProgrammer

Reputation: 52314

If you need user interaction, open /dev/tty, it will be the controlling terminal for the process. Standard error and standard input may be redirected as well.

Upvotes: 1

Philipp T.
Philipp T.

Reputation: 793

When your stdout is piped somewhere else, the only way to show something on the terminal (apart from maybe things like curses and dialog) is stderr.

Upvotes: 1

Related Questions