Re: shell to perl script (103 Views)
Reply
Honored Contributor
sekar sundaram
Posts: 615
Registered: ‎11-14-2008
Message 1 of 6 (103 Views)
Accepted Solution

shell to perl script

hi,
i would like to convert a small shell script to perl.
-------
for NODE in `cat node-list.txt`
do
#run this commnad opcragt $NODE
#and check the output is like this
#Node specified with Primary Node Name is not managed by HPOM for Windows
#write that node name into a file
if [ opcragt $NODE | grep HPOM ]
echo $NODE >> node-not-available.txt
#if yes, mean, node is not managed
----------

thanks...
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,579
Registered: ‎05-19-2003
Message 2 of 6 (103 Views)

Re: shell to perl script

1) I do not have 'opcragt' available so I don't know exactly what the output would look like.
Provide a good/bad example for better help.

2) You may want to use the -all option and parse the output against a pre-read node list.

Check out: http://perlmeme.org/faqs/system/system.html

Anyway... 1) could be done using something like:

# perl x.pl < node-list.txt >> node-not-available.txt
where (untested) ...
-- x.pl --
use strict;
use warnings;
while (my $node = <>) {
chomp $node;
for (qx(opcragt $node)) {
if (/HPOM/) {
print qq($node\n);
last;
}
}
}

Hope this help some,
Hein


Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 6 (103 Views)

Re: shell to perl script

Hi:

You haven't provided a shell script other than in pseudocode. Using 'echo' instead of 'opcragt' to demonstrate, if you had something like:

# cat ./mysh
#!/bin/sh
for NODE in $(< node-list.txt)
do
if [ $(echo $NODE | grep -s HPOM) ]; then
echo "${NODE} not managed"
else
echo "${NODE} managed"
fi
done

...in Perl, we could do:

# cat ./mypl
#!/usr/bin/perl
use strict;
use warnings;
my $result;
while (<>) {
chomp;
$result = `echo $_ | grep -s HPOM`;
if ( $result =~ /HPOM/ ) {
print "$_ not managed\n"
}
else {
print "$_ managed\n"
}
}
1;

...which given an input file named 'node-list.txt' would be run like:

# ./mysh

(or):

# ./mypl node-lst-txt

If the contents of the input looked like:

# cat node-list.txt
node_one
node_two
node_HPOM
node_three

...then the output of either the shell or the Perl script would be:

node_one managed
node_two managed
node_HPOM not managed
node_three managed

You can redirect the output when you run either variation. I prefer to do this rather than embed that logic.

Regards!

...JRF...
Please use plain text.
Honored Contributor
sekar sundaram
Posts: 615
Registered: ‎11-14-2008
Message 4 of 6 (103 Views)

Re: shell to perl script

great, thanks a ton...i will check them... i will provide one opcragt output as well...

one more small grep help...i am having two list of nodes. i want to compare them and make the list of nodes present in fair and missing in app1
ie,
grep $NODE app1-node-list >>present
if $NODE not present in app1 node list, i want to write it in a new file...how to do that please....

root@fair> ll
total 224
-rw-r--r-- 1 root sys 85757 Nov 12 01:47 app1-node-list
-rw-r--r-- 1 root sys 19198 Nov 12 08:25 fair-node-list
root@fair>
root@fair> for NODE in `cat fair-node-list`
> do
> grep $NODE app1-node-list >>present
> done
root@fair> ll
total 256
-rw-r--r-- 1 root sys 85757 Nov 12 01:47 app1-node-list
-rw-r--r-- 1 root sys 19198 Nov 12 08:25 fair-node-list
-rw-r--r-- 1 root sys 14017 Nov 12 09:28 present
root@fair> wc *
7637 7660 85757 app1-node-list
1576 1570 19198 fair-node-list
1192 1192 14017 present
10405 10422 118972 total
root@fair>
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,579
Registered: ‎05-19-2003
Message 5 of 6 (103 Views)

Re: shell to perl script


well, you could just use :

grep -f app1-node-list fair-node-list

Beware that the lines in the first file mentioned are treated as regular expression PATTERNs. Do depending on needs, feeds and platform you may want to add any or all of:
-i, ignore case distinctions
-w, force PATTERN to match only whole words
-x, force PATTERN to match only whole lines


If you need more work to be done, then the following working, rough, framework may help:

perl present.pl app1-node-list fair-node-list

where
----------------- present.pl -------
use strict;
use warnings;
my %first;

my $input_file = shift or die "Need a first file to work on";
open (FILE, "<$input_file") or die "Could not open first file $input_file\n$!";
$first{$_}=1 for ();
print scalar keys %first, " lines in first $input_file\nPresent:\n";

$input_file = shift or die "Need a second file to work on";
open (FILE, "<$input_file") or die "Could not open second file $input_file\n$!";
for () {
print if $first{$_};
}

Enjoy,
Hein
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,579
Registered: ‎05-19-2003
Message 6 of 6 (103 Views)

Re: shell to perl script

Oops, hit 'submit' before adding an explanation to the perl script.

It reads a first file and for each line creates a hash array element with the whole line as key.
For some usages you may want to add a 'chomp' to this to remove the newline, and a uc or lc to force upper or lower case.

Then it reads the second file iterating over that and trying each line as key into the array. If found then print, else onto the next line.
The 'else' could do other stuff.
It could also play futher with the array to create a driver table for the 'opcrag' command.

Or when using opcarg -all, then you migh be able to use the arrays to do the right processing for the nodes reported on.

Cheers,
Hein
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation