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

I KNOW this is the wrong way to do it and I am getting errors on the the last if statement. Can someone help me convert the shell script to perl code? The process ONLY dies with kill 9. Randal said to use +9.

#!usr/local/bin/perl use strict; # kill all silver stream processes my @processes=`ps -ef | grep ThingIwant | awk \'{ print \$2 }\'`; foreach (@processes) { `kill -9 $_` && print "Process $_ killed!"; } # copy the log file and create a new one `cp /local/webserver/bin/log /local/home/admin/temp_log_copy` && print + "performing the copy"; if (-e "/local/home/admin/temp_log_copy" && print "checking the existe +nce of log") { chdir "/local/webserver/bin/"; `rm log` && print "log deleted\n"; `touch log` && print "new log created\n"; `chmod 644 log` && print "log rights changed\n"; `chown root log` && print "changed owner to root\n"; `chgrp other log` && print "changed the group to other\n"; `/bin/sh /local/webserver/bin/startserver.sh` && print "The server is +back up"; } if (-e "/local/home/admin/log"`date '+%m%d%y'|sed s/\ /_/g`"_box_50 & +& print "checking the existence of log") { mv /local/home/admin/temp_log_copy /local/home/admin/log"`date '+%m%d +%y'|sed s/\ /_/g`"_box_50a; } else { mv /local/home/admin/temp_log_copy /local/home/admin/log"`date '+%m%d +%y'|sed s/\ /_/g`"_box_50; } exit;

Ergowolf
Does code make a sound if no one is there to type it?

Replies are listed 'Best First'.
Re: convert shell to perl
by ncw (Friar) on Aug 31, 2000 at 18:46 UTC
    Something like this for the last if
    # This isn't perlish at all but if you need a faithfull emulation... my $date = `date '+%m%d%y'|sed s/\ /_/g`; chomp($date); print "checking the existence of log"; if (-e "/local/home/admin/log${date}_box_50") { rename "/local/home/admin/temp_log_copy", "/local/home/admin/log$ +{date}_box_50a" or die "Failed to rename"; } else { rename "/local/home/admin/temp_log_copy", "/local/home/admin/log$ +{date}_box_50" or die "Failed to rename"; }
    But really this is horrible! Stop trying to program for shell in perl and learn perl properly! I promise you you'll never look back. In fact whenever I'm writing a shell script now-a-days and I'm thinking about a loop or ...| awk {print $1} or something like that I'll reach for perl!
Re: convert shell to perl
by eak (Monk) on Sep 01, 2000 at 07:55 UTC
    Here is a little cleaner way to handle the killing of the processes.
    #!/usr/bin/perl -w use Proc::ProcessTable; my $t = new Proc::ProcessTable; foreach my $p (@{$t->table}){ kill SIGKILL => $p->pid if $p->cmndline and $p->cmndline =~ /netscap +e/; }
    --eric
Re: convert shell to perl
by fundflow (Chaplain) on Aug 31, 2000 at 18:38 UTC
    It seems like you need to concatenate with a . the string in the if:
    if (-e "/local/home/admin/log".`date '+%m%d%y'|sed s/\ /_/g`"_box_50)
    (Or something like that)
Re: convert shell to perl
by b (Beadle) on Sep 01, 2000 at 18:15 UTC
    I found it easy to parse the output of 'top' for things like that.

    top is more standard than ps in terms of output layout and input parameters so your script is more portable.