How to redirect stderr to a file as well


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect stderr to a file as well
# 1  
Old 09-15-2011
How to redirect stderr to a file as well

Hello everyone,

I'm a nooby in Linux, and I need some help.

I have a shell script like this:

Code:
echo "Start of script" > ../My_Log_Dir/Script_Name.log
 
..
 
cp ../My_DataIn/File.txt ../My_DataOut/ 2>> ../My_Log_Dir/Script_Name.log
 
rc=$?
 
..
 
echo "End of Script" >> ../My_Log_Dir/Script_Name.log
 
exit rc

And I execute the Shell like this:
Code:
../My_Shell_Dir/./My_Shell 2>../My_Log_Dir/Temp.log

Later I'll need to read Temp.log so I want it with all the error display's. But as I'm not getting the error display from de cp command. A tryed with the tee command and it worked, but I lost the return code from de cp command.

My idea here is to put the error display in a log file (used in other processes) and still keep it in the stderr. In this way I could use it when I invoque the shell script.

Hope someone can help!

Thank's!

Last edited by vbe; 09-15-2011 at 02:20 PM.. Reason: typos
# 2  
Old 09-15-2011
What do you want? errors and stdout in same file? or errors in one stdout in another?
This User Gave Thanks to vbe For This Post:
# 3  
Old 09-15-2011
Code:
../My_Shell_Dir/./My_Shell 2>../My_Log_Dir/Temp.log

I dont get... Am I to tired and not seeing the obvious or is there some typos errors (the use of slash-dot-slash)
This User Gave Thanks to vbe For This Post:
# 4  
Old 09-15-2011
Instead of redirecting stdout and stderr of every command in your script...why dont you just do it once on the command line when you are executing it...
Code:
echo "Start of script"
..
cp ../My_DataIn/File.txt ../My_DataOut/
..
rc=$?
..
echo "End of Script"
.. 
exit rc

Thats your script with all the stdout and stderr redirection taken out...now when you run it on the comand line just do it as...
Code:
../My_Shell_Dir/./My_Shell >../My_Log_Dir/Temp.log 2>&1

this will send stdout and stderr of every command in your script to Temp.log and you wont lose the return code from any command either.
This User Gave Thanks to shamrock For This Post:
# 5  
Old 09-17-2011
Quote:
Originally Posted by vbe
What do you want? errors and stdout in same file? or errors in one stdout in another?
I want two things:
  1. to produce a log file during the execucion of the script with some output made by myself (the echo cases) and the errors from stderr (from the cp command);
  2. I also need the errors to been set in stderr, so when I execute the shell script in temp.log I get in temp.log error from the cp command (if any) and errors if its not possible to execute the script, because I'm runing the script from a SAS Script in other environment.
Like this I would have 2 log files. One with the execution log (Script_name.log) and some explanations and the other (Temp.log) only with the errors, that I will read after and print that out, if there is any.

As I said erlier I can't perform a tee command because I'll get the return command from tee and not the cp command.

That is why Shamrock solution doesn't fit.

Thank's everyone.
# 6  
Old 09-17-2011
Like this?
Code:
../My_Shell_Dir/./My_Shell >../My_Log_Dir/My_Shell.log 2>../My_Log_Dir/Temp.log

Output logs in My_Shell.log
Error logs in Temp.log

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies

2. Shell Programming and Scripting

How to redirect stderr for <file if file does not exist?

$ a=$(<missing) -bash: missing: No such file or directory $ a=$(<missing) 2> /dev/null -bash: missing: No such file or directory $ a=$(<missing 2> /dev/null) -bash: missing: No such file or directory $ a=$( { <missing 2> /dev/null; } ) -bash: missing: No such file or directory $... (10 Replies)
Discussion started by: Michael Stora
10 Replies

3. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

4. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

5. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

6. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

7. UNIX for Dummies Questions & Answers

Redirect just stderr to a file with a timestamp

I'm using below command to redirect stderr to a file but I also want to add timestamp to stderr.out to find out the date / time the error occurred. ls -ltr 2>>/tmp/stderr.out Thanks (5 Replies)
Discussion started by: mbak
5 Replies

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

9. Shell Programming and Scripting

how can i redirect stderr to file in Make?

Hello all im using tcsh shell on sun Solaris , using the Make utility for compilation i will like to be able to redirect the stderr to file , how can it be done ? (0 Replies)
Discussion started by: umen
0 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