Confused about redirecting stderr


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Confused about redirecting stderr
# 8  
Old 02-17-2019
Well most common use of redirection is more redirecting STDERR to STDOUT:
Code:
 ls -al  . >ls.out 2>&1

I tend to keep both separate specially when using cron, as if in a rush, if you redirect all to error.log you are to read the whole file just to look for a possible issue but if you keep them separate:
Code:
my_cron_job >>my_cron_job.log 2>my_cron_job.err

Now my expectations is to see my_cron_job.err file size = 0 meaning all is well... and no point to look at my_cron_job.log if I am busy as all I will find there are the results of the job that executed correctly as the .err is empty...

Last edited by vbe; 02-18-2019 at 04:09 AM.. Reason: Typos - Thanks Rudi
This User Gave Thanks to vbe For This Post:
# 9  
Old 02-25-2019
I use another form in a way too:-
Code:
echo "This is my debug message" >&2

This way, I can write my own messages to the standard error file (or indeed any other file descriptor I choose to define) which are easier to ignore if called as a function or from another script, e.g.:-
Code:
#!/bin/bash

# Call so_and_so ignoring my errors
so_and_so 3>/dev/null

# Call so_and_so collecting std output & my debug as a variable
my_var=$(so_and_so 3>&1)

These tricks can be useful to put loads of debug info into scripts then ignore them when you are happy without removing them. Of course, you can then see all the useful information you have previously set up quite easily too.




Just my thoughts,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 10  
Old 02-25-2019
Quote:
echo "This is my debug message" >&2
Isn't >&2 the same as 1>&2 , both of which say "Send <stdout> to where <stderr> is pointed towards (the terminal)?
Or, stated another way, "Send 1 to where 2 is going, which by default goes where 1 goes". If so, I can't understand the point of >&2.

Last edited by Xubuntu56; 02-25-2019 at 10:40 AM.. Reason: clarification
# 11  
Old 02-25-2019
Quote:
Originally Posted by Xubuntu56
... which by default goes where 1 goes".
It does not. You need to differentiate between "by default" and "by habit" here. With your interactive sessions, you have both pointing to your terminal by habit. But running a command or script may change that immediately - they may have a different opinion of what their errors should go to. And even more so for system programs, e.g. daemons, which will off the shelf log to log files and print errors to their stderr. And this is what rbatte1 shows: a simple "error log" command for use inside (standalone?) scripts, assuming stdout and stderr have been split when / before calling / running,



Quote:
If so, I can't understand the point of >&2.
echo prints to stdout, so you need to redirect stdout to the stderr file descriptor for the "error log command".




EDIT: LOL - trying to prove my point, I thought I take Xorgas an example, but it has both redirected to /var/log/.../x-0.log Still, many dbus programs have fd 1 pointing to /dev/null and fd 2 to ~/.xsession-errors

Last edited by RudiC; 02-25-2019 at 11:06 AM..
This User Gave Thanks to RudiC For This Post:
# 12  
Old 02-25-2019
Yes, by default stdout and stderr go to the terminal but in different streams.
Each of them can be redirected to an individual target, then they are separatate.
Say you run a script with redirecting stderr to errorfile
Code:
/path/to/script 2> errorfile

Then in the script
Code:
echo "This is my debug message" >&2

goes to errorfile.

You can do all that in the script:
Code:
exec 2>errorfile
echo "error 1" >&2
echo "error 2" >&2

Note that only the "exec" opens the file.
Then each >&2 write goes to the stream, i.e. appends to the file.
Or you can redirect a code block:
Code:
{
echo "error 1" >&2
echo "error 2" >&2
} 2>errorfile
# The code block has ended. The follwoing goes to the original stderr again.
echo "error 3" >&2

Further you can nest code blocks
Code:
{
{
echo "error 1"
echo "error 2"
} >&2
echo "not an error"
echo "error 3" >&2
} 2>errorfile

Last but not least, a if-then-fi or while-do-done or for-do-done or case-in-esac are code blocks.
This often makes sense with a while-do-done loop.
Code:
while read line <&5
do
  echo "$line"
done 5<inputfile

You do not need an explicit block around the while-do-done loop
Code:
{
} 5<inputfile

These 2 Users Gave Thanks to MadeInGermany 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

Lost redirecting stderr & stdout to 3 files - one each plus combined

Hi folks I need/want to redirect output (stdout, stderr) from an exec call to separate files. One for stderr only and two(!) different (!) ones for the combined output of stderr and stdout. After some research and testing i got this so far : (( exec ${command} ${command_parameters} 3>&1... (6 Replies)
Discussion started by: MDominok
6 Replies

2. Shell Programming and Scripting

Preserve output order when redirecting stdout and stderr

Hi, I already searched through the forum and tried to find a answer for my problem but I didn't found a full working solution, thats way I start this new thread and hope, some can help out. I wonder that I'm not able to find a working solution for the following scenario: Working in bash I... (8 Replies)
Discussion started by: Boemm
8 Replies

3. Shell Programming and Scripting

Is there a way to tee stderr from a command that's redirecting error to a file?

I'm not a complete novice at unix but I'm not all that advanced either. I'm hoping that someone with a little more knowledge than myself has the answer I'm looking for. I'm writing a wrapper script that will be passed user commands from the cron... Ex: ./mywrapper.sh "/usr/bin/ps -ef |... (1 Reply)
Discussion started by: sumgi
1 Replies

4. Shell Programming and Scripting

Redirecting STDERR to a file from within a bash script

I am trying to redirect the output from stderr to a log file from within a bash script. the script is to long to add 2> $logfile to the end of each command. I have been trying to do it with the command exec 2> $logfile This mostly works. Unfortunately, when a read command requires that anything be... (5 Replies)
Discussion started by: vockleya
5 Replies

5. Shell Programming and Scripting

noob question about redirecting stderr

I dont know what I am doing wrong but I would like to redirect the stderr output to a file? the specific command is this time wget http://www.something.com/somefile.bin All I want to see is time's output which is stderr so I can see how long the file download took. I've tried redirecting... (2 Replies)
Discussion started by: trey85stang
2 Replies

6. Shell Programming and Scripting

Redirecting stderr problem

% ls -ld /usr /foo ls: /foo: No such file or directory drwxr-xr-x 14 root wheel 512 May 18 02:49 /usr % ls -ld /usr /foo 1>/dev/null/ /dev/null/: Not a directory. % ls -ld /usr /foo 2>/dev/null/ /dev/null/: Not a directory. ^^Why why why doesn't this work for me. Furthermore, where is... (7 Replies)
Discussion started by: phpfreak
7 Replies

7. Shell Programming and Scripting

Confused about redirecting output from awk.

I have a simple script written in awk whose purpose is to go through some php files and replace some strings. Naturally, I want the changes to be written out to the files. The script looks generally as follows: { gsub(/'replacethis'/, "with this"); # a bunch of these print $0 > FILENAME }... (3 Replies)
Discussion started by: face1
3 Replies

8. Shell Programming and Scripting

Redirecting STDERR message to STDOUT & file at same time

Friends I have to redirect STDERR messages both to screen and also capture the same in a file. 2 > &1 | tee file works but it also displays the non error messages to file, while i only need error messages. Can anyone help?? (10 Replies)
Discussion started by: vikashtulsiyan
10 Replies

9. Shell Programming and Scripting

Redirecting stderr while live

If I forget to set up stderr redirection on execution of a script, is there a way to set that redirection post-exec? In other words, if I have a script running and no errors are being logged... and then I remember that I forgot the 2>&1 on the script... can I turn it on after the fact? ...and... (1 Reply)
Discussion started by: jjinno
1 Replies

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