in reply to Re^4: Executing command from perl script with input/output
in thread Executing command from perl script with input/output

Hi, I have run into that "terminal" problem with a few scripts before. What I end up doing is the following "trick". Instead of opening $cmd with IPC::Open3, open "/bin/sh", then print $cmd to it.
my $pid=open3(\*WRITE, \*READ, \*ERROR, '/bin/bash'); print WRITE $cmd;
That usually works, because you are simualting a terminal.

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^6: Executing command from perl script with input/output
by linuxfan (Beadle) on Aug 10, 2005 at 18:26 UTC
    Thanks Zentara,

    I tried the method you suggested above, but this still doesn't work for me :-( After writing the $cmd to WRITE, I created two variables $selread and $selerror (like earlier) - one each to read from READ and ERROR. A read on both of these did not yield anything at all.

    I want to use a debugger to see what is happening, but my program is split over several modules, and I am not sure how to use the debugger for something that spans over many modules.

    Regards,
    ravi

      Oops, I meant to change the $cmd to "$cmd\n", but I hoped you would figure that out. Remember, the bash shell needs a newline (or enter) to start executing the command. Maybe even use 2 print statements, one to print $cmd, and one to print the code for enter(a newline). Additionally you may need to set $|=1; and maybe even try using syswrite instead of print. If none of those things work, you can try to setup a pseudo-tty, which I find quite difficult. See IO::Pty.

      If all fails you can always fall back to using the old standby "expect" instead of IPC::Open3.

      Quite often, it is some little tiny error you are making which prevents you from getting output, even if you have it setup right, and it usually involves buffering and flushing, so the $|=1 is important. If buffering is the problem, you might see the output as you quit the program.


      I'm not really a human, but I play one on earth. flash japh
        Zentara,

        As expected, I printed the "\n" along with $cmd. However, none of the methods you suggested has worked for me :( I guess I will try installing the Expect module and figure out a way to do this.

        Thanks a lot for your help!