CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
If then conditional statement with forward slash

 



perl_is_fun
New User

Oct 22, 2018, 7:17 AM

Post #1 of 3 (911 views)
If then conditional statement with forward slash Can't Post

I'm trying to verify that a process is running. I have this:

Code
#!/usr/bin/perl -w  
use strict;
use warnings;
use POSIX;
use File::Pid;

my $port = "1234";
my $address = "127.0.0.1";

my $dieNow = 0;
my $isrunning = 0;
my $rtlamrpid = 0;
my $rtl = "/usr/sbin/rtl_tcp";

open(PS,"ps -C \"rtl_tcp\" -o pid= -o cmd= |") || die "PS Failed: $!\n";
while (my $row = <PS>) {
print ($row);
if ($row =~ /^\s*([0-9]{1,9})\srtl_tcp -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})/) {
print("match");
if (($3 == $port) && ($2 eq $address)) {
print( "RTL_TCP Already Running @ PID " . $1 );

}
}
}
close PS;


The output from open(PS,"ps -C \"rtl_tcp\" -o pid= -o cmd= |") || die "PS Failed:$!\n"; is:
23443 /usr/bin/rtl_tcp -a 127.0.0.1 -p 1234 -d 0

My code does not see a match. I have tried replacing "\srtl_tcp -a" with [$rtl -a] which gives me a match but it does not match the second if then statement. I have also tried using "\/usr\/sbin\/rtl_tcp" with no luck.

Any help would be appreciated.


Zhris
Enthusiast

Oct 22, 2018, 9:49 PM

Post #2 of 3 (899 views)
Re: [perl_is_fun] If then conditional statement with forward slash [In reply to] Can't Post

Hi,

None of your regexps will match your string properly:


Code
use strict; 
use warnings;

my $string = '23443 /usr/bin/rtl_tcp -a 127.0.0.1 -p 1234 -d 0';
my $rtl = '/usr/sbin/rtl_tcp';
my $rtl_b = '/usr/bin/rtl_tcp';
my $regexps =
[
# original
qr#^\s*([0-9]{1,9})\srtl_tcp -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,
qr#^\s*([0-9]{1,9})[$rtl -a] (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,
qr#^\s*([0-9]{1,9})\s\/usr\/sbin\/rtl_tcp -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,

# modified
# path must be matched too
qr#^\s*([0-9]{1,9})\s(?:(?:\/[a-z]+)+\/)?rtl_tcp -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,
# path must not be in character class and /sbin must be /bin
qr#^\s*([0-9]{1,9}) $rtl_b -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,
# /sbin must be /bin
qr#^\s*([0-9]{1,9})\s\/usr\/bin\/rtl_tcp -a (.*) -p ([0-9]{1,5}) -d ([0-9]{1,2})#,
];

$, = "\n";
$\ = "\n\n";
print $_, $string =~ $_ for @$regexps;


Chris


perl_is_fun
New User

Oct 23, 2018, 10:58 AM

Post #3 of 3 (886 views)
Re: [Zhris] If then conditional statement with forward slash [In reply to] Can't Post

That solved the issue.
Thanks.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives