1752290 Members
4616 Online
108786 Solutions
New Discussion юеВ

shell script to perl

 
SOLVED
Go to solution
Dani_Galera
Occasional Advisor

shell script to perl

hello,

i need to recreate a shell script into a perl one (so i can run it on Wintel)
my Perl knowledege is poor. can you help to recreate these lines?

*****************************************

TS=$(date "+DATE: %m/%d/%y TIME: %H:%M:%S")

# print TS to all check_* printers: ----------------------------#

for CHECK in `cat $DZL_ETC/dzl_check-dsm-jqm.cfg`
do
echo $TS | pdpr -x"-job-priority 0" -d $CHECK > /dev/null 2>&1
done

# verify if printout is succesful -------------------------------------#

sleep 10 # give the printers some seconds to finish

for CHECK in `cat $DZL_ETC/dzl_check-dsm-jqm.cfg`
do
if $(grep -q "$TS" $DZL_LOG/check_dsm_jqm/$CHECK)
then echo "$CHECK OK $TS"
else echo "$CHECK NOK $TS"
cat /dev/null > $DZL_LOG/check_dsm_jqm/$CHECK #empty file
fi
done

*****************************************

thanks to all
6 REPLIES 6
Peter Nikitka
Honored Contributor

Re: shell script to perl

Hi,

though you could get advise how to this port from ksh/Posix-SH to perl, I really recommend learning Perl.
The port of this script, which should be easy, is a good opportunity to do so.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: shell script to perl

Hi:

I agree with Peter. You should begin your journey learning Perl. You could start here:

http://perldoc.perl.org/perlintro.html

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor
Solution

Re: shell script to perl

I agree with both posters. Also see Wendy's link on how to learn Perl (Wendy is an all-time perl beginner, so she would know :)

http://perlmonks.org/index.pl?node_id=283154



--8<--- Untested translation
use strict;
use warnings;

my @now = localtime;
# Using braindead US date format is left as an exercise to the reader
my $TS = sprintf "DATE: %4d-%02d-%02d TIME: %02d:%02d:%02d", $now[5] + 1900, $now[4] + 1, @now[3,2,1,0];

# print TS to all check_* printers
my @cfg;
{ local @ARGV = ("$ENV{DZL_ETC}/dzl_check-dsm-jqm.cfg");
chomp (@cfg = <>);
}
foreach my $CHECK (@cfg) {
open my $pr, qq{pdpr -x"-job-priority 0" -d $CHECK >/dev/null 2>&1 |" or die "$_: $!\n";
print $pr "$TS\n";
close $tr;
}

# verify if printout is succesful

sleep (10); # give the printers some seconds to finish

foreach my $CHECK (@cfg) {
my $file = "$ENV{DZL_LOG/check_dsm_jqm/$CHECK";
local @ARGV = ($file);
print "$CHECK $TS ", (grep m/\b$TS\b/ => <>) ? "OK\n" : "NOK\n";
truncate $file, 0; # empty file
}
--->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Dani_Galera
Occasional Advisor

Re: shell script to perl

i created the perl script with the help of procura. but now i have a little problem:

i have now this:

@jqm_tmp = `nmcp list | grep jqm | grep ids | grep -v hosts | cut -f1 -d","`;

print jqm_cfg "@jqm_tmp";

normally the result of 'nmcp ...' is something like:

dsm_fax01
dsm_fax03
dsm_mail01
dsm_01a
dsm_03b

but in the log file (jqm_cfg) i have something like:

dsm_fax01
dsm_fax03
dsm_mail01
dsm_01a
dsm_03b

do you know how can i remove the blank spaces before the words?

regards
James R. Ferguson
Acclaimed Contributor

Re: shell script to perl

Hi Dani:

The Forum tends to obliterate your formatting unless you check the "retain formatting" box at the bottom of your post.

However, I think you want:

# print jqm_cfg @jqm_tmp;

...instead of the interpolated list (with double-quotes).

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: shell script to perl

Hihi :) :)
All those blanks get lost in space here on ITRC unless you tick the Retain Format checkbox

That grep is so un-perl!

my @jqm_tmp = map { m/^\s*(.*?)\s*,/; $1 } grep { m/jqm/ && m/ids/ && !m/hosts/ } => `nmcp list`;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn