By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Redirecting error output to a File

P: 5
I have a script as below
system("ls>output"), this will write the result to a file output.However if the command fails it throws the error on the screen. How can we redirect the error to a file. I need to redirect both success & failure log in a file

[Linux ~]$ perl
ls: No such file or directory

Any help or pointers will be helpful.

Mar 10 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 100+
P: 589
Input/Output Redirection
Mar 10 '10 #2

P: 4
Expand|Select|Wrap|Line Numbers
  1. system("ls output >out 2>err");
Here 2 represents STDERR.
Mar 11 '10 #3

Expert Mod 2.5K+
P: 3,503
Allow me to also put in my .02. When asking a question in a coding forum, it really helps if you paste the code you are using in code tags so that those trying to help you aren't guessing at what "might" be wrong.

Looking at the output you posted, it says "". That tells me you have typo's that need fixing.


Mar 12 '10 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. system("dir >>output.txt");
  2. system("dir >>output.txt 2>>output.txt");
Iam running a system command and need to pass both error and success to same log file. Here system command is in a loop with different commands,. I tried as above but still not working.
Mar 14 '10 #5

Expert Mod 2.5K+
P: 3,503
First, I fixed your code tags. They are like HTML, you open the tag, then put what you want inside them, then you close them. You put two open/close sets, one before and one after, which does absolutely nothing.

As for your issue, you were almost correct. Try this:

Expand|Select|Wrap|Line Numbers
  1. system("dir >>output.txt 2>output.txt");
You want to redirect the output of standard error (2) to the same as standard out (>> output.txt). You should just use one '>' instead of the append (>>).

See if that works for you.


Mar 14 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.