stderr/stdout


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stderr/stdout
# 1  
Old 05-09-2011
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 would like to uncomment the first diff and get
that to write to stderr. Any examples would be greatly appreciated.
This needs to work with ksh.....

Thanks in advance to all who answer

Code:
cat /tmp/t1.ksh
============
 
#!/usr/bin/ksh
CheckForError()
{
   ERR_NUM=$1
   touch alert_${ERR_NUM}.now  alert_${ERR_NUM}.prv
   grep ${ERR_NUM} /tmp/alert.log > /tmp/alert_${ERR_NUM}.now
   ##diff /tmp/alert_${ERR_NUM}.prv /tmp/alert_${ERR_NUM}.now | grep '>'| cut -c3-
   diff /tmp/alert_${ERR_NUM}.prv /tmp/alert_${ERR_NUM}.now
}
main()
{
    for error_num in ORA-04030 ORA-04088
    do
      CheckForError $error_num
    done
} >$OUTPUT_FILE 2>$ERR_FILE
OUTPUT_FILE=/tmp/junk1.out
ERR_FILE=/tmp/junk1.err
main
return 0

 
cat /tmp/alert.log
==============
 
ORA-04030: out of process memory when trying to allocate 8528 bytes
ORA-04030: out of process memory when trying to allocate 2072 bytes
ORA-04030: out of process memory when trying to allocate 1280 bytes
ORA-04088: error during execution of trigger 'XXX.INS_TRIG'

# 2  
Old 05-09-2011
You need to use the ampersand notation. Like this:
Code:
$ ( echo hello ) 2> errfile
hello
$ cat errfile
$
$ ( echo hello >&2 ) 2> errfile
$ cat errfile
hello
$
$

# 3  
Old 05-09-2011
Try this:

Code:
diff /tmp/alert_${ERR_NUM}.prv /tmp/alert_${ERR_NUM}.now 1>&2

# 4  
Old 05-09-2011
Thanks guys, I always use 2>&1 it never occured to me to change the
order.
# 5  
Old 05-10-2011
Quote:
Originally Posted by Perderabo
You need to use the ampersand notation. Like this:
Code:
$ ( echo hello ) 2> errfile
hello
$ cat errfile
$
$ ( echo hello >&2 ) 2> errfile
$ cat errfile
hello
$
$

ineresting solution, can you explain it as now I am curious.

I looks like in the parenthesise you redirecting the word "hello" to
stderr but what does this do 2>errfile or why is it needed?

Thanks
# 6  
Old 05-10-2011
parentheses create a subshell and I sent the subshell's stderr to errfile. Then in the subshell I show an echo command both without and then with the needed redirection.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Programming

stderr stdout to a log file

I originally wrote my script using the korn shell and had to port it to bash on a another server. My script is working find for backing up but noticed that now after the move, I am not getting any output to my log files. Using Korn shell, this worked for me for some odd reason. This was sending... (2 Replies)
Discussion started by: metallica1973
2 Replies

3. Red Hat

Redirect STDOUT and STDERR of chsh

EDIT: Nevermind, figured it out! Forgot to put backslashes in my perl script to not process literals! Hi everyone. I am trying to have this command pass silently. (no output) chsh -s /bin/sh news Currently it outputs. I've tried.... &> /dev/null 1> /dev/null 2>&1 /dev/null 1>&2... (1 Reply)
Discussion started by: austinharris43
1 Replies

4. Red Hat

Make STDERR readable as STDOUT

Hi all. I am trying to use backticks in Perl to put STDERR into a string. The code is... $readkey_test = `perl -MTerm::ReadKey -e 1`; print $readkey_test; if ($readkey_test =~ m/]/) { print "ReadKey not installed...\n"; } else { print "ReadKey installed...\n"; } If it comes up... (3 Replies)
Discussion started by: austinharris43
3 Replies

5. Shell Programming and Scripting

sending stdout and stderr to a file

working on a c sell script I think I understand the concept of it, which is: filename >> file.txt (to appaend) or filename | tee -a file.txt (to append) The problem is that my shell script is used with several parameters, and these commands don't seem to work with just filename. They... (2 Replies)
Discussion started by: mistermojo
2 Replies

6. 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

7. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

8. Shell Programming and Scripting

redirecting STDOUT & STDERR

In bash, I need to send the STDOUT and STDERR from a command to one file, and then just STDERR to another file. Doing one or the other using redirects is easy, but trying to do both at once is a bit tricky. Anyone have any ideas? (9 Replies)
Discussion started by: jshinaman
9 Replies

9. 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

10. 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
Login or Register to Ask a Question