Operating System - HP-UX
1826018 Members
2924 Online
109690 Solutions
New Discussion

Convert Perl Screpit into Shell

 
SOLVED
Go to solution
Salman AlBedaiwi
Frequent Advisor

Convert Perl Screpit into Shell

I tried allot to transfer the follwoing code to shell but i couldnt .
Array is My problem i couldnt code it in shell

#!/opt/OV/bin/Perl/bin/perl
@nodes = `/opt/OV/bin/OpC/utils/opcnode -list_nodes | grep Name | cut -f 11 -d " " | grep -v MGTSV`;
chomp(@nodes);

# ping nodes to check theis status
use Net::Ping ;

$p = Net::Ping->new("icmp");

foreach $host (@nodes)
{
$mon = 1 ;
$mon = 0 unless $p->ping($host, 5);
if ( $mon == 0 )
{
$mon = 1;
$mon = 0 unless $p->ping($host, 15);
}
system `/opt/OV/bin/OpC/opcmon check_node_status=$mon -object $host`;
}
$p->close();

Can anyone help me please
Sincerely
Salman A AlBedaiwi
6 REPLIES 6
Peter Nikitka
Honored Contributor

Re: Convert Perl Screpit into Shell

Hi,

why do you want to do this?
If this script is working: use it as it is or modify it to meet your needs (e.g. setup a list which is NOT a result of an OpC-command).

If this script is not working: tell us about the error message. I suspect this construction:
system `/opt/OV/bin/OpC/opcmon check_node_status=$mon -object $host`;

The combination of backtic and the system command seems strange: the system would execute the string returned by the opcmon command ...

If you make yourself clear - write it down! - what this script is doing, it won't be a problem to use any (scripting) language to get it working.

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: Convert Perl Screpit into Shell

Hi Salman:

All that is happening here is that a list of hosts is being 'ping'ed; first with a 5-second timeout; and then (if that fails) with a 15-second timeout. The status of the ping is then passed to an external script called 'opcmon'.

This is definitely more robust in Perl than in a shell script. The construction of the '@nodes' array is decidedly un-Perlish. You could do:

my ($fh, @nodes);
open( $fh, "-|", "opt/OV/bin/OpC/utils/opcnode -list_nodes" ) or die;
while (<$fh>) {
next if /MGTSV/;
my @fields = split;
push @nodes, $fields[10];
}
...

That is, Perl counts 0-relative, so field-11 in your spawned process (the 'cut') is field-10 when done in Perl.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Convert Perl Screpit into Shell

Hi (again):

Oops, I dropped the match for "Name":

my ($fh, @nodes);
open( $fh, "-|", "opt/OV/bin/OpC/utils/opcnode -list_nodes" ) or die;
while (<$fh>) {
next unless /Name/;
my @fields = split;
next if /MGTSV/;
push @nodes, $fields[10];
}

...

Regards!

...JRF...
George Spencer_4
Frequent Advisor

Re: Convert Perl Screpit into Shell

In shell you could use the "set" command to create an array. This works fine when the list is separated by whitespace.

e.g. set -A hosts ` your code `

This creates an array starting with ${host[0]} and with ${#host[*]} entries.

If the hosts are separated by commas, the you will have to use the FS variable to split it correctly. Make sure to save the previous value before changing, then restore afterwards.
Salman AlBedaiwi
Frequent Advisor

Re: Convert Perl Screpit into Shell

I keept the script as it is
it was taking so many times to run again due to increasing the number of nodes i take out some nodes to let it be checked by other script so i make them working in together and my problem solved
thank allot for all of you
Sincerely
Salman A AlBedaiwi
Peter Nikitka
Honored Contributor

Re: Convert Perl Screpit into Shell

Hi,

you should take look at
http://forums13.itrc.hp.com/service/forums/helptips.do?#33

It's a good practise here in our forum...

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"