Help - Search - Member List - Calendar
Full Version: Wrapper script
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Errin Larsen
Hi Perl folks,

I am trying to write a wrapper script around an executable file so
that I am able to better control the results of other programs calling
that file. I have written a short and easy script that collects the
options in a log file, and then execs the original command (just for
testing).


#!/usr/bin/perl

use strict;
use warnings;

my $datestampe = localtime(time);

open LOG, ">>/tmp/options.log" or die "Can't open /tmp/options.log:
$!";
print LOG "$datestamp: Command and options: $0 @ARGS";
exec( '/usr/bin/original-executable', @ARGS);


However, I have found that some open the original file in a pipe. Is
there a way I can view the contents of what is being sent through the
pipe?

Thanks,

--Errin

Errin M HMMA/Information Technol
Hi Perl folks,

No one replied to my posts about a wrapper script. I'm going to try
again.

I want to be able to process some command-line options, then call/exec
a separate command with my processed options, AND pass along the piped
input to that command as well.

For example, if I have a command called /bin/print-something, I would
normally call it like this:

# /bin/print-something -a -b some-option -c someother-option

But, I want to move that command to /bin/original-print-something, and
then replace it with my script. My script will read in those options
(maybe change them, maybe log something, whatever) and then exec the
original-print-something script with the now processed options. This
all works fine:


#/usr/bin/perl

use strict;
use warnings;

my @processed_args;

open LOG, ">>/tmp/wrapper.log" or die "Couldn't open /tmp/wrapper.log:
$!";

while( @ARGV ) {
# Here is where the args would be checked/changed/tweaked
push @processed_args, $new_arg;
}

print LOG "Contents of @processed_args: @processed_argsn";

exec '/bin/original-print-something', @processed_args or die "Couldn't
exec /bin/original-print-something: $!";



My problems come when I get a command-line like this:
# cat /etc/hosts | /bin/print-something -a -b some-option -c
someother-option

In this case, my LOG file will recored the args as "-a -b some-option -c
someother-option", but not the piped input. Where did it go? Is there
a variable that will contain that piped data so I can manipulate that
stuff, too?

--Errin

Ryan Frantz
QUOTE
exec '/bin/original-print-something', @processed_args or die
"Couldn't
exec /bin/original-print-something: $!";



My problems come when I get a command-line like this:
# cat /etc/hosts | /bin/print-something -a -b some-option -c
someother-option

In this case, my LOG file will recored the args as "-a -b some-option
-c
someother-option", but not the piped input.  Where did it go?  Is
there
a variable  that will contain that piped data so I can manipulate that
stuff, too?


I'm just a beginner myself, but you may want to look into the diamond
operator, <>. From the little that I've learned about (and used) it,
you can use it to write code that will support input/output piping. If
you've got a copy of 'Learning Perl' there's some information on it.
I'm sure there are web docs too.

QUOTE
--Errin

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


----- Original Message -----
From: "Larsen, Errin M HMMA/Information Technology Department" <[Email Removed]>
Date: Tuesday, June 28, 2005 11:25 am
Subject: RE:Wrapper Script

QUOTE
Hi Perl folks,
Hello Errin

No one replied to my posts about a wrapper script.  I'm going to try
again.

I want to be able to process some command-line options, then
call/execa separate command with my processed options, AND pass
along the piped
input to that command as well.

For example, if I have a command called /bin/print-something, I
wouldnormally call it like this:

# /bin/print-something -a -b some-option -c someother-option

But, I want to move that command to /bin/original-print-
something, and
then replace it with my script.  My script will read in those options
(maybe change them, maybe log something, whatever) and then exec the
original-print-something script with the now processed options.  This
all works fine:


#/usr/bin/perl

use strict;
use warnings;

my @processed_args;

open LOG, ">>/tmp/wrapper.log" or die "Couldn't open
/tmp/wrapper.log:$!";

while( @ARGV ) {
# Here is where the args would be checked/changed/tweaked
push @processed_args, $new_arg;
}

print LOG "Contents of @processed_args: @processed_argsn";

exec '/bin/original-print-something', @processed_args or die
"Couldn'texec /bin/original-print-something: $!";



My problems come when I get a command-line like this:
# cat /etc/hosts | /bin/print-something -a -b some-option -c
someother-option

In this case, my LOG file will recored the args as "-a -b some-
option -c
someother-option", but not the piped input.  Where did it go?  Is
therea variable  that will contain that piped data so I can
manipulate that
stuff, too?
I think the problem may be in the function that process your ARGS. I am refereing to the function which sets $new_args. You would need to showe us some more code to get more help.


QUOTE
while( @ARGV ) {
# Here is where the args would be checked/changed/tweaked
push @processed_args, $new_arg;
}

Mark G.
QUOTE

--Errin

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response





PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.