Perl script to store device serial number and inst

2019-08-26 21:58发布

问题:

What I want to do is to capture the device serial numbers and store them in an array or list. Then I want to install my apk on various android devices that I connect to my system.I am trying to make a perl script that can do this for me.

Something like this:

if($ostype eq 'MSWin32') {

  system("title Android");

    $adbcommand_devices = "adb devices";

    $adbcommand_install = "adb -s xxxxxxxx install HelloWorld.apk";
}

  if(`adb -s xxxxxxxx get-state` =~ m/device/i) {
          system($adbcommand_devices);            
          system($adbcommand_install);

         }
 else {
    print "Device is offline\n";
}

The serial number should come from the currently connected device.

回答1:

Here is an example of just the adb devices command using IPC::Run3:

use strict;
use warnings qw(all);

use IPC::Run3;
use Carp qw(croak confess cluck);
use Data::Dumper;

my $ADB_PATH = '/path/to/adb';  # EDIT THIS

my @devices = get_devices();
print Dumper(\@devices);
exit 0;

# subs
sub get_devices {
    my $adb_out;
    run3 [$ADB_PATH, 'devices'], undef, \$adb_out, undef;
    $? and cluck "Warning: non-zero exit status from adb ($?)";

    my @res = $adb_out =~ m/^([[:xdigit:]]+) \s+ device$/xmg;
    return wantarray ? @res : \@res;
}

For a lot of this, you may be able to use qx/`` as well. E.g., you could replace the run3 with my $adb_out = `$ADB_PATH devices`; (because you didn't need to pass anything in to it, just out, and also didn't need to avoid the shell.)



标签: android perl adb