Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Count script wrapper help

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 05-01-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by richsark View Post
OK, I follow you, so what do you recommend?
One way could be to install a Perl module like IO::CaptureOutput (or others) to explicitly capture stderr as mentioned in the following webpage:

Run external process from Perl, capture stderr, stdout AND the process exit code - Stack Overflow

I haven't worked with any of the modules mentioned therein, and my current setup does not allow me to install them. I can try them later on in my personal laptop later on.

In the meantime, maybe a forum member could come up with yet another suggestion.

HTH,
tyler_durden
Sponsored Links
    #16  
Old Unix and Linux 05-01-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
HI

You think thats the issue?
Sponsored Links
    #17  
Old Unix and Linux 05-01-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by richsark View Post
HI

You think thats the issue?
Yes, check this:


Code:
$
$ # the following command executes successfully
$
$ ls -1 *.jpg
file1.jpg
file2.jpg
$
$ # now when I use back-ticks to run the command,
$ # @x gets assigned the output, or "stdout" of this command
$
$ perl -e '{ @x = `ls -1 *.jpg`; }'
$
$ # it didn't print anything because I did not add the "print"
$ # command explicitly; if I do, then:
$
$ perl -e '{ @x = `ls -1 *.jpg`; print @x; }'
file1.jpg
file2.jpg
$
$ # it prints properly
$
$ # Now this command does not execute successfully
$
$ ls -1 *.bmp
ls: cannot access *.bmp: No such file or directory
$
$ # The message that you see is standard error, or "stderr"
$ # It does not get assigned to @x, as you can see below
$
$ perl -e '{ @x = `ls -1 *.bmp`; }'
ls: cannot access *.bmp: No such file or directory
$
$ # I did not add the "print" command explicitly, still I see
$ # the output - that's because it is the "stderr" thrown by
$ # the command itself. It never got assigned to @x, as the
$ # following one-liner shows.
$
$ perl -e '{ @x = `ls -1 *.bmp`; print "Value of @x => ",@x; }'
ls: cannot access *.bmp: No such file or directory
Value of  => $
$
$

So, essentially you'll have to capture the standard output (stdout) as well as the standard error (stderr) in your perl program. The "back-tick" method can capture stdout, but cannot capture stderr. That's why you'd need those additional modules mentioned in the link above.

Once you've captured both, you'd check stdout for success and stderr for failure, and proceed from there.

HTH,
tyler_durden
    #18  
Old Unix and Linux 05-05-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
Hi,

I got it worked out with the help of many kind folks.

If your interested, I can post the final code?

Thanks
Sponsored Links
    #19  
Old Unix and Linux 05-05-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by richsark View Post
Hi,

I got it worked out with the help of many kind folks.

If your interested, I can post the final code?
Sure, go ahead.

tyler_durden
Sponsored Links
    #20  
Old Unix and Linux 05-05-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
Here it is.

I want to thank Durden, FishMonger, Adam and ozo


Code:
#!perl
use strict; 
use warnings; 
use Data::Dumper; 
 
open my $log, '>', 'log-external.txt' or die "Could not open log: $!"; 
print $log "Subnet,Static,DHCP,Unused\n"; 
 
open my $dump, '>', 'dump.log' or die "failed to open 'dump.log' $!"; 
 
##### Step 1, read subnets 
open my $in, '<', 'm-names.txt' or die "Could not open m-names.txt: $!\n"; 
while( my $subnet = <$in>) { 
 
    print "Checking $subnet"; 
    chomp $subnet; 
     
    my %counts = (  
                   Static => 0, 
                   DHCP   => 0, 
                   Unused => 0, 
    ); 
     
    my @dnsoptions = `./getobjectlst.exe -u xx -p xx -o rich -a $subnet 2>&1`;
 
    print $dump "dumping \@dnsoptions\n";     
    print $dump Data::Dumper->Dump(\@dnsoptions); 
    print $dump '=' x 25, "\n"; 
 
    if ( @dnsoptions and $dnsoptions[0] eq '' ) {
        print "dnsoptions is null or undefined, going on to the next subnet\n"; 
        next; 
    } 
 
    # Now, at this point, we may have "Error 48" in @dnsoptions, or we may have 
    # the nicely formatted output. We'll have to check for both cases here. 
    # Let's check the unsuccessful case first. The condition below checks if 
    # the first element of @dnsoptions array has the following text in it - 
    # "Error 48: This subnet does not exist." in it. 
 
 
    if (join("",@dnsoptions) =~ /Error 48: This subnet does not exist./) { 
 
        # call "getsubnetlst.exe", passing $subnet as one of the parameters 
        my @subnetpart2 = `./getsubnetlst.exe -u xx-p xx -o rich -a $subnet -t network`; 
 
        print $dump "dumping \@subnetpart2\n";     
        print $dump Data::Dumper->Dump(\@subnetpart2); 
        print $dump '=' x 25, "\n"; 
 
         
        # now loop through each element of the array @subnetpart2, which looks like this - 
        # ########################################################################## 
        # "East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128" 
        # ########################################################################## 
        # pick up the 2nd field from the left (e.g. 146.149.1.0 above), and pass it as 
        # a parameter to the cli "getobjectlst.exe". 
 
 
        foreach my $line ( @subnetpart2 ) { 
 
            # get the 2nd field from the left 
            my $snetpart2 = (split/"\s+"/, $line)[1];
             if( $snetpart2 =~ /"/ ){
             warn "\$snetpart2 was $snetpart2\n when \$line was $line";
             next;  
} 
            # and now pass it to "getobjectlst.exe"; 
            my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -o rich -a $snetpart2`;
 
            print $dump "dumping \@dnsoptions2\n";     
            print $dump Data::Dumper->Dump(\@dnsoptions2); 
            print $dump '=' x 25, "\n"; 
             
            # find out counts of each subnettype (4th field from left) 
            foreach my $line (@dnsoptions2) { 
                my @subnettype = split/"\s+"/, $line; 
                $counts{$subnettype[3]}++; 
            } 
        } 
    } 
    else { # successful output from getobjectlst.exe 
 
        # find out counts of each subnettype (4th field from left) 
        foreach my $line (@dnsoptions) { 
            my @subnettype = split/"\s+"/, $line;
            $counts{$subnettype[3]}++; 
        } 
    } 
    printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused}; 
} 
 
close($in); 
close($log);

Only thing I like to solve is the output format,

I would like the output from the log-external.txt
from this:

Subnet,Static,DHCP,Unused
10.0.0.0/8
,37,0,217
146.149.0.0/16



To this:


Subnet,Static,DHCP,Unused

10.0.0.0/8,37,0,217
146.149.0.0/16,174,0,39314
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
wrapper script in perl richsark Shell Programming and Scripting 5 03-23-2009 04:41 PM
Help with a wrapper script not working richsark Shell Programming and Scripting 0 03-09-2009 01:49 PM
Korn Shell Wrapper script pareshan Shell Programming and Scripting 6 12-23-2008 12:56 PM
What is a wrapper script thana UNIX for Dummies Questions & Answers 1 01-28-2008 07:11 AM
What is wrapper script and how to write chiru UNIX for Dummies Questions & Answers 1 06-12-2006 05:23 AM



All times are GMT -4. The time now is 11:05 AM.