Redirecting error output to a File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • briggs
    New Member
    • Dec 2009
    • 5

    Redirecting error output to a File

    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
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Input/Output Redirection
    Linux follows the philosophy that every thing is a file. A keyboard, monitor, mouse, printer ... are all files in Linux. Linux identifies ...

    Comment

    • kiruthikaU
      New Member
      • Feb 2010
      • 4

      #3
      Code:
      system("ls output >out 2>err");
      Here 2 represents STDERR.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • briggs
          New Member
          • Dec 2009
          • 5

          #5
          Code:
          system("dir test.pl >>output.txt");
          system("dir t.pl >>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.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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:

            Code:
            system("dir t.pl >>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.

            Regards,

            Jeff

            Comment

            Working...