RE: Want to interactively run a shell script



Apologies this shows how to write also

#!/usr/local/bin/perl -w
use Data::Dumper;
use File::Copy;
use IPC::Open3;
use IO::Select;
use IO::Handle;
use strict;

use vars qw(
$informixserver $archive $informixdir
$archivedir $line $select
$read $write $error
);
$informixserver = $ENV{INFORMIXSERVER};
$informixdir = $ENV{INFORMIXDIR};
$archive = '';
$archivedir = '';


( $select, $read, $write, $error ) = process_command('command name here');
process_prog($select);
close_handles( $read, $write, $error );

sub process_command {
my ($command) = @_;

my ( $pid, $readbuff, $timeout, $s );
$timeout = 10;
$read = new IO::Handle;
$write = new IO::Handle;
$error = new IO::Handle;

$pid = open3( $write, $read, $error, $command );

#
# create selects
#
$s = IO::Select->new();
$s->add($write);
$s->add($read);
$s->add($error);

return ( $s, $read, $write, $error ); }

sub process_prog {
my ($select) = shift;
my ( @ready, $handle, $timeout, $readbuff, $flag, $tapeloc, $newnum );
$flag = 0;

#
# get write handle
#

my ($write) = ( $select->can_write($timeout) )[0];
while ( $flag == 0 ) {
@ready = $select->can_read($timeout);

foreach $handle (@ready) {
sysread( $handle, $readbuff, 256 );

if ( length($readbuff) == 0 ) {
$flag = 1;
}
}
print "$readbuff\n" if($readbuff=~/\S+/);

if ( $readbuff =~ /something here/ )
{
syswrite $write, "0\n";
}

sleep(1);
}
}

sub close_handles {
my ( $r, $w, $e ) = @_;
$r->close();
$e->close();
$w->close();
}

Please note this hasn't been run.

-----Original Message-----
From: Andrew Curry [mailto:andrew.curry@xxxxxxxxxxxx]
Sent: 04 September 2007 15:57
To: ppp ppp; beginners@xxxxxxxx
Subject: RE: Want to interactively run a shell script

Something along this usually works to interact with programs through perl,
this could be edited for your purpose.
This does a simple tail but you can guess the rest.

#!/usr/local/bin/perl -w
use Data::Dumper;
use File::Copy;
use IPC::Open3;
use IO::Select;
use IO::Handle;
use strict;

use vars qw(
$line $select
$read $write $error
);

( $select, $read, $error ) =
process_command('/usr/bin/tail -f tmp.dat');
process_tail($select);
close_handles( $read, $error );

sub process_command {
my ($command) = @_;

my ( $pid, $readbuff, $timeout, $s );
$timeout = 10;
$read = new IO::Handle;
$error = new IO::Handle;

$pid = open3( $write, $read, $error, $command );

#
# create selects
#
$s = IO::Select->new();
$s->add($read);
$s->add($error);

return ( $s, $read, $error ); }

sub process_tail {
my ($select) = shift;
my ( @ready, $handle, $timeout, $readbuff, $flag, $tapeloc, $newnum );
$flag = 0;

#
# get write handle
#

while ( $flag == 0 ) {
@ready = $select->can_read($timeout);

foreach $handle (@ready) {
sysread( $handle, $readbuff, 256 ); # 256 is the number of chars
to read.
print "$readbuff";

if ( length($readbuff) == 0 ) {
$flag = 1;
}
}
#
# do something here
#
}
sleep(1);
}

sub close_handles {
my ( $r, $e ) = @_;
$r->close();
$e->close();
}




-----Original Message-----
From: ppp ppp [mailto:prabhat_xyz@xxxxxxxxx]
Sent: 04 September 2007 15:50
To: beginners@xxxxxxxx
Subject: Want to interactively run a shell script


Hi All,

I want to remove some packages install on remote solaris through
script. I am able to run simple command like ls -l ,ls through perl script
by using Net::Telnet module.

Now I want my script should remove all the packages installed on remote
solaris

means i want to remove

pkgrm -R `pwd` command but the problem is that it requires user to
manually enter yes/no to remove the pkgs

suppose i will issue pkgrm -R `pwd` then it asks

The follwing pkgs are install do u want to remove this package[y]
here I required to enter y
then it ask
do u want to remove xxxx.pkg ?
here also I required to enter y

I need a way so that my script will send those and I don't have to enter
commands

how my script will be interactive so that i can enter y through my script
I want to run my script on my PC and the packages are installed on a
remote Solaris box.

Thanks


---------------------------------
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel
and lay it on us.


This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.





--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/



This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.




.



Relevant Pages

  • Re: Newbie "undefined value"
    ... > used strict and -w. ... From that same command line, if I pass in parameters, ... > to print to a file and using parts of Stein's guestbook script. ... > sub write_datafile { ...
    (comp.lang.perl.misc)
  • Re: command for script
    ... I did not use the script for a while and now I am not able to find out ... That line above is, in fact, the command you should use to run your ... close OUT1; ... sub parse { ...
    (comp.lang.perl.misc)
  • Re: Cycle through variables
    ... the same command for all variables without copying pasting the command 20 ... Below is an example of my current script - I'd really ... Define objTextFile and objTextFile2 outside of the Sub ...
    (microsoft.public.scripting.vbscript)
  • Newbie "undefined value"
    ... From that same command line, if I pass in parameters, ... I thought "strict" would catch all ... I had a short script that printed to the browser. ... sub write_datafile { ...
    (comp.lang.perl.misc)
  • Tool to get MCP ODT at Windows CMD prompt
    ... You basically send a series of ODT commands of your choosing, once, or in a loop, and get the output right in the command ... Dim sHostname ... Sub WriteIt ... WriteIt "" ...
    (comp.sys.unisys)