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

Hi,

I'm trying to capture all output from an external interactive process to a log file.

For example, I would like perl to execute "cp -i file1 file2" and log the overwrite prompt and user input when file2 exists.

I have managed to find a solution by calling a system("cp -i file1 file2 2>&1 | tee out.log"); but as I require the exit code from the cp this isn't feasible :(

I hope someone can help, I'm close to tearing my hair out over this one!

  • Comment on How to capture log from external interactive process

Replies are listed 'Best First'.
Re: How to capture log from external interactive process
by ambrus (Abbot) on Sep 17, 2004 at 11:47 UTC

    Did you try script? It's a standard bsd program which is probably already on your machine. It logs every character output to the terminal (including those echoed from the input). If I understand you correctly, that will do exactly what you want. See script(1).

Re: How to capture log from external interactive process
by tbone1 (Monsignor) on Sep 17, 2004 at 12:39 UTC
    If you are shell scripting on a Unix box, and your system call is tantamount to that, the exit code/return value of the command-just-run is stored in the variable '$?'. Perl does a similar thing with this variable. NOTE: because it changes every time a child process is run, if you want to preserve this value, you need to save it off, a la this shell sniplet:

    rc=$? if [[ ${rc} -ne 0 ]]; then echo "cp -i $file1 $file2 failed with a value of ${rc}" fi

    Hope this helps.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      rc=$? if [[ ${rc} -ne 0 ]]; then echo "cp -i $file1 $file2 failed with a value of ${rc}" fi

      Thank you for reminding me of why I shall never, ever learn shell scripting and shall forever stick with Perl. :)

        Thanks for your help guys, but I'm still stumped.

        Script may well work but I need to do all this from a perl script.

        1) execute external program from perl
        2) using perl or shell scripting magic, log the programs output and user input
        3) use perl to check the exit status of the program

        I've tried using perl's open3, but then stdout/stderr/stdin all get piped to perl :(

Re: How to capture log from external interactive process
by johnnywang (Priest) on Sep 17, 2004 at 16:39 UTC