Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to set font color for STDIN,STDOUT and STDERR? Post 302542402 by Corona688 on Wednesday 27th of July 2011 12:27:16 PM
Old 07-27-2011
The terminal can't tell what's printing to standard output vs standard error -- they both go to literally the same file. You could intercept standard error with a fifo and process it...
Code:
#!/bin/bash

# On exit, close stderr and delete the fifo so we don't leave dangling background processes.
trap "exec 2<&- ; rm ~/.tmpfifo" EXIT

rm -f ~/.tmpfifo

mkfifo ~/.tmpfifo
# Create a subshell that reads from fifo, writes to stderr.
# it gets its own copy of stderr, so redirecting stderr later won't change this.
(       while read LINE
        do
                printf "\x1b[01;31m%s\x1b[01;m\n" "$LINE"
        done < ~/.tmpfifo ) &

exec 2>~/.tmpfifo

# Thing that should print standard error in red
touch /

Code:
$ ./errfilter.sh
touch: setting times of `/': Permission denied
$

This only works for scripts, not for interactive terminals, because bash frequently uses stderr for its own things when in interactive mode.

---------- Post updated at 10:27 AM ---------- Previous update was at 10:11 AM ----------

Actually, here's a way that'll work in interactive mode:

Code:
trap "exec 5>~/.tmpfifo-$$ ; rm -f ~/.tmpfifo-$$ ; exec 5<&-" EXIT

mkfifo ~/.tmpfifo-$$

(       while [ -e ~/.tmpfifo-$$ ]
        do
                while read LINE
                do
                        printf "\x1b[01;31m%s\x1b[01;m\n" "$LINE"
                done < ~/.tmpfifo-$$ >&2
        done
 ) &

function err
{
        "$@" 2>/.tmpfifo-$$
}

Do this in your shell:
Code:
$ source errfilter.sh
$ err touch /
touch: setting times of `/': Permission denied
$

and any commands you run with err will have stderr color-coded.
This User Gave Thanks to Corona688 For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies

2. Shell Programming and Scripting

precedence of stderr and stdout

#!/usr/bin/perl open(STDOUT, ">>$Textfile") open(STDERR, ">>$Textfile") print "program running\n"; $final = join("+", $initial,$final) #5 close (STDOUT); close (STDERR);Hi all, above is my perl code. Notice i have captured the stdout and stderr to the same textfile. my code is expected to... (1 Reply)
Discussion started by: new2ss
1 Replies

3. Programming

stdout/stdin + flushing buffers

Hi all I've run into a snag in a program of mine where part of what I entered in at the start of run-time, instead of the current value within printf() is being printed out. After failing with fflush() and setbuf(), I tried the following approach void BufferFlusher() { int in=0;... (9 Replies)
Discussion started by: JamesGoh
9 Replies

4. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

5. Shell Programming and Scripting

can't close stdin/stdout in shell

#!/bin/sh exec 0</dev/null exec 1>/dev/null ls -l /proc/self/fd >&2 produces total 0 lr-x------ 1 tyler users 64 Feb 18 10:38 0 -> /proc/7886/fd lrwx------ 1 tyler users 64 Feb 18 10:38 1 -> /dev/pts/4 lrwx------ 1 tyler users 64 Feb 18 10:38 2 -> /dev/pts/4 I've verified the shell is... (10 Replies)
Discussion started by: Corona688
10 Replies

6. Shell Programming and Scripting

stderr/stdout

Can somebody explain to me why the diff output is not going to stderr? Yet when I issue a diff from the command line the return code is -ne 1. I am guessing diff always writes to stdout??? Is there away I can force the difff to write to stderr USING THE CURRENT template. If possible, I... (5 Replies)
Discussion started by: BeefStu
5 Replies

7. UNIX for Dummies Questions & Answers

STDIN and STDOUT

Hallo, i have a script like: if ;then echo "OK" else echo "ERROR $2 is missing" fi; if ;then touch $2 fi; if ;then cat $1 | grep xy > $2 (1 Reply)
Discussion started by: eightball
1 Replies

8. Shell Programming and Scripting

stdout, stderr redirection

Hi all, can someone help me with the next redirection? i want to redirect the stdout+stderr of a command to the same file (this i can do by prog &> file) but in addition i want to redirect only the stderr to a different file. how can i do this please? (in BASH) thanks. (4 Replies)
Discussion started by: eee
4 Replies

9. Shell Programming and Scripting

Redirect string from bash stderr to user stdin

Hi there, I need to execute a command in the bash. The program prints some standard (output and) error and then wants the user to choose one of several options and type the according input. I am trying to solve this issue in a bash script but also running into some circular dependency. How can I... (7 Replies)
Discussion started by: fredestet
7 Replies
Appender::Screen(3)					User Contributed Perl Documentation				       Appender::Screen(3)

NAME
Log::Log4perl::Appender::Screen - Log to STDOUT/STDERR SYNOPSIS
use Log::Log4perl::Appender::Screen; my $app = Log::Log4perl::Appender::Screen->new( stderr => 0, utf8 => 1, ); $file->log(message => "Log me "); DESCRIPTION
This is a simple appender for writing to STDOUT or STDERR. The constructor "new()" take an optional parameter "stderr", if set to a true value, the appender will log to STDERR. The default setting for "stderr" is 1, so messages will be logged to STDERR by default. If "stderr" is set to a false value, it will log to STDOUT (or, more accurately, whichever file handle is selected via "select()", STDOUT by default). Design and implementation of this module has been greatly inspired by Dave Rolsky's "Log::Dispatch" appender framework. To enable printing wide utf8 characters, set the utf8 option to a true value: my $app = Log::Log4perl::Appender::Screen->new( stderr => 1, utf8 => 1, ); This will issue the necessary binmode command to the selected output channel (stderr/stdout). COPYRIGHT AND LICENSE
Copyright 2002-2009 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.1 2010-02-07 Appender::Screen(3)
All times are GMT -4. The time now is 10:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy