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
hi,
I have a script as below

test.pl
system("ls test.pl>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 test.pl
ls: est.pl: No such file or directory
[Linux~]$

Any help or pointers will be helpful.

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


Expert Mod 100+
P: 589
Input/Output Redirection
http://linuxhelp.blogspot.com/2006/0...simple-in.html
Mar 10 '10 #2

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

numberwhun
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 "est.pl.....". That tells me you have typo's that need fixing.

Regards,

Jeff
Mar 12 '10 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. system("dir test.pl >>output.txt");
  2. system("dir t.pl >>output.txt 2>>output.txt");
  3.  
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

numberwhun
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 t.pl >>output.txt 2>output.txt");
  2.  
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.

Regards,

Jeff
Mar 14 '10 #6

Post your reply

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