Young Young - 1 year ago 43
Perl Question

Perl: Best way to have two conditions (of Linux shell commands) in the until loop

I'm trying to find the best way to include 2 conditions of running Linux shell commands inside the while or until loop.

Pretty much, I can think of either of the following ways:

until ( (`who |grep user1`) && (`last -5 |grep user2`) ) {
print "Waiting...\n";


open(my $command1, "who |"); open(my $command2, "last -5 |");
my $status1=1; my $status2=1;
until ( ($status1 == 0) && ($status2 == 0) ) {
while(<$command1>) { $status1=0 if /user1/; }
while(<$command2>) { $status2=0 if /user2/; }
print "*** Waiting....... \n";
close($command2); close($command1);

I guess there might be better Perl ways to do the above.

Any help would be appreciated.

mob mob
Answer Source

Perl has its own grep function and it is usually better to use it than the shell command

until (grep(/user1/,`who`) && grep /user2/,`last -5`) {

but you can also pattern match against the raw output of a command, so grep might be superfluous

until (`who` =~ /user1/ && `last -5` =~ /user2/) {