Append stderr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append stderr
# 8  
Old 05-19-2010
Thank you scottn and sorry for having bumped the thread up.
I've tryed to adapt your script to my needs, without success.

I have:
Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE}"
    done
}

${PROGRAM}  >> "${LOG_FILE_1}" 2>&1 >> ${LOGFILE_2} | log

But the scripts stops in reading lines and doesn't go ahead...

Which changes should I make?
# 9  
Old 05-19-2010
Hi.

This bit is wrong:
Code:
${PROGRAM} >> "${LOG_FILE_1}" 2>&1 >> ${LOGFILE_2} | log

Should be
Code:
${PROGRAM} 2>&1 >> ${LOGFILE_2} | log

If you want to write to two log files, you should combine this with tee:
Code:
${PROGRAM}  2>&1 | tee -a "${LOG_FILE_1}" >> ${LOGFILE_2} | log

I just ran this on ksh and bash on AIX, Linux and Solaris, and sh on Solaris, and it works the same on all of them - but only when feeding log through a pipe - not calling it directly. What OS/Shell are you using?
# 10  
Old 05-19-2010
I'm working on a Red Hat Enterprise 4 AS with GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu).

I tryed your script but I obtain either stdout and stderr in both log files...
# 11  
Old 05-19-2010
Maybe it would be best if you showed your script - exactly what you are doing - including where you set variables, like LOGFILE, LOG_FILE_1, LOGFILE_2, PROGRAM, etc...
# 12  
Old 05-19-2010
Are you sure following helped you to capture stderr into output.log log file?
It appears you are directing stdout to output.log in a non-appending mode.
Code:
./myScript &> output.log

Did you try this ?
Code:
./myScript & 1>>output.log 2>>errors.log

Your code -->

Code:
${command}  >> "${globalLogFile}"

How about this ?
Code:
${command}  >> "${globalLogFile}" 2>>"${globalerrorLogFile}

# 13  
Old 05-20-2010
The closest solution I have tryed is the following:
Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
    done
}

${command}  >> "${LOGFILE_1}" 2>>"${LOGFILE_2} | log

I obtain the stdout inside $LOGFILE_1 and the stderr inside $LOGFILE_2 BUT without the date before it, as I wanted to.
Simply the log function is not called at all.

Variables $LOGFILE_1 and $LOGFILE_2 are something like
Code:
LOGFILE_1=/home/canduc/file1.log
LOGFILE_2=/home/canduc/file2.log

, so nothing special.

$command is an executable written in C a bit hard to explain what it does...but it prints strings either on stdout and stderr, I think it is enough to describe my problem.

I tryed also with
Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
    done
}

${command} 2>>"${LOGFILE_2} | log

but I obtain the stdout (with date) in the log file and
Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" 2>> "${LOGFILE_2}"
    done
}

${command} 2>>"${LOGFILE_2} | log

(notice the "2>>" inside the function), but I obtain the stderr inside the log, WITHOUT the date.

I think that the problem can be summarized into this question: how to pass the stderr to a function?

Thank you for your patience...

Last edited by canduc17; 05-20-2010 at 06:19 AM..
# 14  
Old 05-20-2010
Hi.

There is no point in directing output of standard error in your log function using 2>> because there never is any.

Code:
SomeFunction 2>&1 >> ${LOGFILE_2} | log

  1. Standard error is directed to where standard output is going at this point in time (i.e. the screen, standard output)
  2. Standard output is then sent to LOGFILE_2
  3. Standard error (still pointing to where standard output was pointing before it was changed) (i.e. the screen) is piped into the log function
  4. The log function should write standard output - not standard error - to LOGFILE

Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
    done
}

${command}  >> "${LOGFILE_1}" 2>>"${LOGFILE_2} | log

All you are doing here is writing standard output to LOGFILE_1 and standard error to LOGFILE_2. There is nothing left for the log function to do - you left nothing to be piped into it.


Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
    done
}
${command} 2>>"${LOGFILE_2} | log

Here, you have sent standard error to LOGFILE_2 (you're missing a quote on that line), standard output goes to the log function, which you also write to LOGFILE_2


Code:
log() {
    while read LINE; do
        echo "$(date) - ${LINE}" 2>> "${LOGFILE_2}"
    done
}

${command} 2>>"${LOGFILE_2} | log

And here, you have written standard error to LOGFILE_2, standard output goes to the log function, but you do nothing with it, as you're directing standard error to LOGFILE_2 (although there is nothing in standard error to write to the file)


Code:
$ cat Test1
LOGFILE_1=L1.txt
LOGFILE_2=L2.txt
LOGFILE=ERR.txt

log() {
    while read LINE; do
        echo "$(date) - ${LINE}" 2>> "${LOGFILE}"
    done
}

SomeFunction() {
  echo "This line goes to log file"
  echo "This line goes to error file" >&2
}

SomeFunction 2>&1 >> ${LOGFILE_2} | log


echo "Log"
cat $LOGFILE_2
echo
echo
echo Error
cat $LOGFILE
echo
echo
echo


$ ./Test1
Log
This line goes to log file


Error

Code:
$ cat Test2
LOGFILE_1=L1.txt
LOGFILE_2=L2.txt
LOGFILE=ERR.txt

log() {
    while read LINE; do
        echo "$(date) - ${LINE}" >> "${LOGFILE}"
    done
}

SomeFunction() {
  echo "This line goes to log file"
  echo "This line goes to error file" >&2
}

SomeFunction 2>&1 >> ${LOGFILE_2} | log


echo "Log"
cat $LOGFILE_2
echo
echo
echo Error
cat $LOGFILE
echo
echo
echo

$ ./Test2
Log
This line goes to log file


Error
Thu May 20 12:42:20 DFT 2010 - This line goes to error file

This User Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt regarding stderr

Hi All, I am writing a shell script code. and i want the stderr to be send to a file and the stdout to be displayed in terminal. In my shell script code i use a read command to get data from user.read -r -p "Enter the type :" data and while i execute my script i use./my_script.sh 2>... (4 Replies)
Discussion started by: Vinoth R
4 Replies

2. Shell Programming and Scripting

Bash - proper way to append variable to stderr

Hello, Can you please if the bellow is the proper way of appending a variable to the stderr: The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results: -rw-r--r-- 1 ab owner 0 Sep 14 13:45 file1 -rw-r--r-- 1 ab owner 0 Sep... (7 Replies)
Discussion started by: alex2005
7 Replies

3. Solaris

can't get stderr port

Hi Experts, i have a solaris 9 OS and i get the following message repeated many time in my /var/adm/messages : Oct 31 16:30:44 baobab rsh: can't get stderr port: Cannot assign requested address have you any idea how can i resolve this issue ??:confused: thanks for help (2 Replies)
Discussion started by: lid-j-one
2 Replies

4. UNIX for Dummies Questions & Answers

how to get stderr

Hello I try to store stderr into a variable, then if this var is not empty i send an email and stop my script. I think my problem is due of "<$dump" into my command line. my bad command line (see samples below on this post) if ! $returnedStr ; then echo ERROR READING DUMP: ... (8 Replies)
Discussion started by: giova
8 Replies

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

6. Shell Programming and Scripting

Can I pipe stderr to another process

Hi there, I was wondering if it was possible to pipe stderr to another process. I need to eval commands given as arguments and I would like to redirect stderr to another process. I can redirect stderr to a file like this... toto:~$ command="one=1" toto:~$ eval $command 2> error toto:~$... (5 Replies)
Discussion started by: chebarbudo
5 Replies

7. Shell Programming and Scripting

STDERR output

Hi, Need some help here on a script I'm writing. I know that STDERR is normally done is this manner: script 2>stderr.out However, if I wanted to output the stderr from a rsh command how do I do that? Example: su - username -c "rsh $hostname /opt/gilberteu/scriptname" 1>stdout... (5 Replies)
Discussion started by: gilberteu
5 Replies

8. Programming

stderr in background process

Herez the question, In a process which writes into file FILE1 with descriptor fHandler1 and it is run as a background process where would statements be directed when stderr descriptor is used. fprintf(stderr,"some message\n"); assume that session from which it is run is terminated and... (3 Replies)
Discussion started by: matrixmadhan
3 Replies

9. UNIX for Dummies Questions & Answers

stderr redirection

Does anyone know away of redirecting the stderr for a bourne or korn shell script to a file. (5 Replies)
Discussion started by: blakmk
5 Replies

10. Programming

stderr

in fprint(stderr, "lkjalsdi\n"); what does stderr mean? thanks (1 Reply)
Discussion started by: dell9
1 Replies
Login or Register to Ask a Question