Append stderr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append stderr
# 1  
Old 05-18-2010
Append stderr

Hi everybody.

I was used to redirect stderr to a file in this way, calling a generic script:
Code:
./myScript &> output.log

But now I need something more sophisticated...Inside a bash script I launch an executable in this way:
Code:
${command}  >> "${globalLogFile}"

So I redirect the stdout into globalLogFile.
How to contemporary redirect the stderr to another log file, APPENDING it?

Thanks in advance!
# 2  
Old 05-18-2010
Something like:

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

# 3  
Old 05-18-2010
Ok, thanks, pretty simple...

And is it possible to give the stderr as input to a fuction?

For example, I have this function:
Code:
log() {
    echo "$(date) - $1" >> "output.log"
}

Is it possible to do something like (pseudocode):
Code:
${command}  >> "${globalLogFile}" 2>> log "&2"

or something?

---------- Post updated at 08:36 AM ---------- Previous update was at 05:01 AM ----------

No ideas?

Moderator's Comments:
Mod Comment Do not bump posts - THANK YOU.

Last edited by Scott; 05-20-2010 at 05:23 AM..
# 4  
Old 05-18-2010
time zones are funny things, aren't they...?

Code:
-> ls -ltr nowhere.txt 2>&1 |tee -a /tmp/error_test.log
nowhere.txt: No such file or directory

-> cat -n /tmp/error_test.log                          
     1  nowhere.txt: No such file or directory
     2  nowhere.txt: No such file or directory

# 5  
Old 05-18-2010
Excuse me, but I do not understand your answer...No ideas for the stderr/log stuff?
# 6  
Old 05-18-2010
This is a forum more than a chat, so patience is sometimes needed before you get a response.

Meanwhile, you can capture your STDERR into your STDOUT using something like my sample, or using your own sample:
Code:
$(command) 2>&1 |tee -a [log.file]

This will allow your error to echo to screen, while it also appends to the named file.
# 7  
Old 05-18-2010
Hi.

Here's an example, of what I think you are asking.
Code:
$ cat LogTest
LogError() {
  while read LINE; do
    echo "ERROR:  $LINE"
  done
}

PrintSomething() {
  echo "OK"
  echo "ERROR1" >&2
  echo "ERROR2" >&2
}

PrintSomething 2>&1 > some_log_file.txt | LogError


$ ./LogTest
ERROR:  ERROR1
ERROR:  ERROR2


$ cat some_log_file.txt
OK

Errors go through function LogError - and from there you can do with it what you wish.
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