Append stderr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append stderr
# 15  
Old 05-20-2010
Thank you so much scottn!
Now my script works as I wanted to and I have definetely better understood bash redirection.

I've reimplemented your last example in a way more similar to my case: instead of producing strings on stdout and stderr with bash, I produce them with a C program, then executed by a script.

Here's the C prog:
Code:
#include <stdio.h>
#include <unistd.h>

int main()
{
    int i;

    for(i=0; i<3; i++)
    {
        fprintf(stdout, "This is message n. %d on the standard output\n", i);
        fprintf(stderr, "This is message n. %d on the standard error\n", i);
        sleep(1);
    }

    return 0;
}

After the compilation, I have executed it with the following script:
Code:
#!/bin/bash

COMMAND="stdoutAndstderr"
OUTLOG="stdout.log"
ERRLOG="stderr.log"

log()
{
    while read LINE; do
        echo "$(date +"%D %T") - ${LINE}" >> ${ERRLOG}
    done
}

$(pwd)/${COMMAND} 2>&1 >> ${OUTLOG} | log

echo "${OUTLOG}:"
cat ${OUTLOG}
echo ""
echo "${ERRLOG}:"
cat ${ERRLOG}

exit 0

And the output is
Code:
 $ ./stdoutAndstderr.sh
stdout.log:
This is message n. 0 on the standard output
This is message n. 1 on the standard output
This is message n. 2 on the standard output

stderr.log:
05/20/10 15:18:52 - This is message n. 0 on the standard error
05/20/10 15:18:53 - This is message n. 1 on the standard error
05/20/10 15:18:54 - This is message n. 2 on the standard error

Hope to be helpful for someone!
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