Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I want to redirect the output of a terminal cmd on linux to a output file . How can i do it ?? I tried the following without much sucess..
open (temp_1, ">>/home/administrator") || die ""; my $rc = `evince bill.pdf`; #runs the command close (temp_1);

I am not able to append the output of the command to the file temp_1.. Help

Replies are listed 'Best First'.
Re: PDF output to temp file
by kcott (Archbishop) on Mar 22, 2012 at 07:57 UTC

    This statement does what you're describing:

    `evince bill.pdf >> /home/administrator/temp_1`;

    Although, I'm not sure that you've really described what you want to do. For instance, what is $rc for?

    In any event, you should take a look at the documentation for the open function. Your code indicates that you don't really understand how this works: the first argument is a filehandle not the filename; the filename (possibly as a full pathname) goes where you have the administrator's home directory. You're likely to break something with code like this!

    -- Ken

      `evince bill.pdf >> /home/administrator/temp_1`;
      Using backticks here doesn't make sense, because due to the redirection, no output would be returned anyway. A better solution would be either
      system('evince bill.pdf >> /home/administrator/temp_1');
      or
      my $errmsg=`evince bill.pdf 2>&1 >> /home/administrator/temp_1`;
      Another alternative to consider is IPC::Run. This would spawn the command without the need of an auxiliary shell process.

      -- 
      Ronald Fischer <ynnor@mm.st>
        Using backticks here doesn't make sense, because due to the redirection, no output would be returned anyway.

        I disagree. AM said that he wanted the output redirected: "I want to redirect the output of a terminal cmd on linux to a output file .". I wasn't about to make guesses based on his code. I did, however, point out issues with the code and queried his use of $rc.

        -- Ken

      It gives me this error when it try to run the above command -->Cannot parse arguments: Cannot open display:

        I hadn't come across evince before but find I have it on my system (Mac OS X):

        $ perl -Mstrict -Mwarnings -E '`evince --help >> fred`;' $ cat fred Usage: evince [OPTION...] [FILE…] GNOME Document Viewer ...

        As you can see, I successfully redirected the output of the command to an output file. This is what you asked for (which I queried).

        Given that I now know that this launches a document viewer, I'm wondering exactly what sort of output you're hoping to redirect to a file.

        I probably can't be much help with the error you're getting except to suggest checking that your X Server is running correctly and $ENV{DISPLAY} has an appropriate value.

        -- Ken