MikG
MikG

Reputation: 1019

Using Perl to cleanly exit a STDOUT dialog after string found

I should admit that I posted a related question to my problem earlier. However I have moved a step further from my initial problem, which is now more Perl related than ADB command related. My thanks to my last poster who suggested I look into Expect functionality, however I am unable to successfully install this module on Win7 platform. I have since changed my original script.

I have the following script below, where I can capture the output of an Android phones ADB logcat text whilst performing a manual PLMN scan on the handset. And the script will log logcat until the specified string is found (string >> /EVENT_NETWORK_SCAN_COMPLETED/ <<) then the log stops. The script works to here, however from this point the STDOUT text is scrolled across my CMD window, and then hangs at the point of the found string, but with no C:> prompt, so to exit I need to perform a manual Ctrl+C on the keyboard. Ideally I'd prefer the STDOUT scrolled text not to be displayed, and simply posted to a text file, then return to Command prompt in my CMD window. So sorry for the similar post, I just seem to be going around in circles at the moment.

use warnings;
use diagnostics;
use strict;

open my $fh, '>', "output.txt" or die "Cannot open output.txt: $!";

open( README, "adb logcat |" ) or die "Can't run program: $!\n";
while (<README>) {
    print $fh "$_\n";
    if ( $_ =~ /EVENT_NETWORK_SCAN_COMPLETED/ ) { 
        system('adb logcat -d');
        system('exit');
    }   
}
close $fh;

Upvotes: 2

Views: 173

Answers (1)

AKHolland
AKHolland

Reputation: 4445

You'll need to kill the process to stop reading from it. This will look something like:

my $pid = open my $log, "-|", "adb logcat";
while(<$log>) {
    $fh->print($_);
    last if m/EVENT_NETWORK_SCAN_COMPLETED/;
}
kill "TERM", $pid;
close $log;

Upvotes: 2

Related Questions