in reply to Re^2: open2 with windows diskpart
in thread open2 with windows diskpart

Worked fine for me simply by adding the missing "\n" I mentioned.

I had problem detecting the prompt because it's not sent until the line is ended (which doesn't happen until someone types since the output appears to be line buffered). I solved that by forcing the prompt to show up twice.

use strict; use warnings; use IO::Handle qw( ); use IPC::Open2 qw( open2 ); sub wait_for_prompt { my ($to_chld, $fr_chld) = @_; # Force two prompts to appear. print $to_chld "\n"; my $count; while (<$fr_chld>) { print "$.: $_"; last if /^DISKPART> $/ && ++$count == 2; } } { my $pid = open2(my $fr_chld, my $to_chld, 'diskpart'); #print $to_chld "\n"; #wait_for_prompt($to_chld, $fr_chld); print $to_chld "list disk\n"; wait_for_prompt($to_chld, $fr_chld); print $to_chld "exit\n"; wait_for_prompt($to_chld, $fr_chld); close $to_chld; waitpid($pid, 0); }

Replies are listed 'Best First'.
Re^4: open2 with windows diskpart
by Anonymous Monk on Jun 17, 2009 at 02:50 UTC

    I think I must be either missing something in my perl distro or maybe my OS ver is messing this up for me. I still can't seem to get this working.

      Then I guess yours isn't line buffered. You'll need to convince diskpart it's talking to a terminal.

        I stepped thru my run of your example and it makes it through to the waitpid, but doesn't complete. I tried waiting for the $pid with &WNOHANG, but it still appears to hang. Since it kinda goes into a black-hole and I can't see what its actually hanging on, I don't know quite how to handle this. I tried this on 32/64b 2008 and I get the same results. 5.8.824. I can try upgrading to a newer 5.10 build if you think that will get this working..it will also serve me well to build a case for using newer Perl builds with the curmudgeon that insists 5.8.8 is the only build that "works properly".