leostereo has asked for the wisdom of the Perl Monks concerning the following question:

hi guys, I would like ask why my script is not working as expected, everthing is working good excep the line that exec system ......, it is strange but if I exec the line on a separate script it works and do the job, any help would be preciated.. following , the code

#!/usr/bin/perl -w use strict; use File::Tail; my $file = File::Tail->new( name =>'/var/log/dhcpd.log', interval => 1, maxinterval => 1, resetafter=> 5, ); while (defined(my $line=$file->read)) { print $line,"\n"; if ($line =~ /^(.*) mdz-shaper1 dhcpd: DHCPACK on ([0- +9\.]+) to ([[:xdigit:]:]+).* ([0-9\.]+)/) { my ($date,$client_ip,$client_mac,$master_ip) = + ($1,$2,$3,$4); system "sed -i '/$client_mac/d' /opt/btb_webservice/ve +r3/lease_list.txt"; if(($client_ip)&&($client_mac)&&($master_ip)){ system 'echo "'.$date.' '. $client_ip.' '.$client_mac.' +'. $master_ip.'"'.' >> /opt/btb_webservice/ver3/lease_list.txt'; } } } 1;

Replies are listed 'Best First'.
Re: tail script
by NetWallah (Canon) on Dec 09, 2015 at 23:11 UTC
    Is the file "/opt/btb_webservice/ver3/lease_list.txt" writable ?

    Mixing shell and perl without error checking is error prone.

    I'd recommend using the perl module File::Inplace to replace your "sed" call, using something like (untested):

    use File::Inplace; my $editor = File::Inplace::->new( file => "/opt/btb_webservice/ver3/lease_list.txt"); while (my ($line) = $editor->next_line) { $line =~/$client_mac/ or next; $editor->replace_line(""); # Zap line } $editor->commit;

            Our business is run on trust. We trust you will pay in advance.

Re: tail script
by jcb (Parson) on Dec 09, 2015 at 22:52 UTC

    Your code uses system twice, which one is not working?

    While sed is complex enough that writing that out in Perl may be more work than it is worth, you should consider the two-argument form of system when calling it.

    The use of system merely to append a line to a file sticks out, however. I suggest using open instead:

    open LIST, '>>', '/opt/btb_webservice/ver3/lease_list.txt' or die "/op +t/btb_webservice/ver3/lease_list.txt: $!"; print LIST join(' ',$date,$client_ip,$client_mac,$master_ip),"\n" or d +ie "/opt/btb_webservice/ver3/lease_list.txt: $!"; close LIST or die "/opt/btb_webservice/ver3/lease_list.txt: $!";