LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-17-2007, 04:55 AM   #1
SPF
Member
 
Registered: Jul 2007
Location: /home
Distribution: Debian
Posts: 36

Rep: Reputation: 15
redirecting stderr to file


Hi,

I want to store bash's debug information into a file like this:
bash -x bashscript.sh 2>&1 > debug.log

But when I open debug.log, the stderr info isn't there.

example:

$ cat example.sh
#!/bin/bash
echo hi
$ bash -x example.sh 2>&1 > debug.log
+ echo hi
$ cat debug.log
hi
$

It seems that there is a hidden channel that outputs "+ echo hi" to the terminal. I tried to find it, but I was unsuccessful.
 
Old 12-17-2007, 04:59 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
The order is important. If you redirect standard error to standard output without previously redirecting standard output to a file, standard error still goes to the terminal. The following should work
Code:
bash -x bashscript.sh > debug.log 2>&1
 
Old 12-17-2007, 05:02 AM   #3
SPF
Member
 
Registered: Jul 2007
Location: /home
Distribution: Debian
Posts: 36

Original Poster
Rep: Reputation: 15
it still confuses me.
 
Old 12-17-2007, 05:20 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Why? When you specify
Code:
2>&1
the number 2 and 1 are the file descriptors of standard error and standard output respectively. A file descriptor is an identifier which is assigned by the shell to keep track of open files (you may consider standard input, output and error as files for these purposes). When the shell reads your command line it executes things in the order you have placed them. So, first
Code:
2>&1   # standard error is redirected to standard output
       # and since standard output is still redirected to
       # the terminal the standard error is redirected to
       # the terminal too (unesuful, because it was already)
second
Code:
> some_file   # equal to 1> some_file
              # redirects standard output to a file, but you're
              # not telling anything about standard error, that
              # is kept redirected to the terminal
In other words, the shell does not put redirection together simply because they appear on the same command line. Instead, redirections are executed in sequence: it is your care to specify them in the correct order. I hope it's a little more clear, now.
 
Old 12-17-2007, 06:56 AM   #5
SPF
Member
 
Registered: Jul 2007
Location: /home
Distribution: Debian
Posts: 36

Original Poster
Rep: Reputation: 15
So:
bash -x bashscript.sh 2>&1 > debug.log

is basicly the same as:
bash -x bashscript.sh > debug.log

because errors go to the terminal by default.

I see it this way, because you are already writing to the file (because it is at the front of the command), all other redirections that come later, pointing to channel 1, will be added to the file (because channel 1 is already being redirected).

Thanks for the explanation colucix!
 
  


Reply

Tags
bash, debug, stderr


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting STDERR iceman_san Linux - Newbie 13 08-10-2007 02:20 AM
redirecting stderr to a pipe dasy2k1 Linux - General 3 03-22-2007 11:28 AM
Redirecting error messages to stderr and file in Bash lowpro2k3 Programming 1 04-14-2005 06:47 PM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
redirecting stderr nodger Programming 2 03-11-2004 10:58 AM


All times are GMT -5. The time now is 07:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration