I'm writing command line utility for Linux. If the output (stdout) is going to a shell it would be nice to print some escapes to colorize output. But if the output is being redirected those bash escapes shouldn't be print, or the content might break parsers that rely on that output.
There are several programs that do this (suck as ack
) but the ones I found were written in Perl and I couldn't find out how they did it.
I wanted to use C/C++ to write my utility.
Before the C shell executes a command, it scans the command line for redirection characters. These special notations direct the shell to redirect input and output. You can redirect the standard input and output of a command with the following syntax statements: Item.
A redirection operator is a special character that can be used with a command, like a Command Prompt command or DOS command, to either redirect the input to the command or the output from the command.
The > symbol is known as the output redirection operator. The output of a process can be redirected to a file by typing the command followed by the output redirection operator and file name.
Regular output append >> operator This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications. txt file by using >> operator.
In (non-standard) C, you can use isatty(). In perl, it is done with the -t operator:
$ perl -E 'say -t STDOUT' 1 $ perl -E 'say -t STDOUT' | cat $
In the shell you can use test:
$ test -t 1 && echo is a tty is a tty $ (test -t 1 && echo is a tty ) | cat $
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With