Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interacting with command line program 1

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
I'm asking on behalf of a coworker so forgive me if I'm light on some details.

Simply said, backticking to execute a command line program. And it hangs. It hangs, because if you had executed this program yourself from the command line it would have thrown up a prompt and asked for some input.

This input cannot be sent to the program as a command line argument.

So, two questions....
1) Is there a way to read this prompt?
2) Is there a way to write to this prompt?

I'm stumped... I figure it's all just streams of stdin and stdout, but once the command line program starts, the perl code stops running, so I'm not sure where to go.

Thanks,
Rob
 
Rob -

you could use the system() function.

What this will do is actually pauses the perl program executes the command outside the program and then starts the program again. For instance.

`command`;
would become

system ("command");

This sould allow you to interact with the command and then carry on with the perl script.

- Ryan
 
But I don't want to interact with the program... I want the perl script to interact with it.
 
First question, is this on a windows box? It may be difficult if this is the case. However, Activestate does have an Expect for Windows available. Due to some OS limitations, I've not seen an expect perl module for windows.

If it's on *nix, either the tcl based expect (which would require learning a little bit of tcl), or the perl expect module.
 
what's the program, it might already have some way of interacting similar to ftp -s where you can specify a cript for the program to take paramaters

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
So this command would require some interaction if you were to just execute it outside of perl? Have you looked at using the Expect Module from CPAN?


"Expect.pm is built to either spawn a process or take an existing filehandle and interact with it such that normally interactive tasks can be done without operator assistance."

Now Expect.pm is really for use where there is no simple way to interact with whatever it is you are trying to do. If you are doing something simple or Common like FTP for instance you can get a module built just for that. But if this is some uknown program or whatever you are trying to interact with Expect.pm would be a way of handling this.

What is it you are doing exacally?

-Ryan
 
It's a proprietary application so there won't be any modules to interact with it... but expect looks promising, I'll forward that on to the guy with the script. Thank you.
 
Put the input that the command expects in a file named "myresponse".
[tt]
system("command <myresponse");
[/tt]
 
You could also open a pipe to the process and then print the response into the pipe, e.g.
Code:
my $cmd ="yourcommandhere";
open(MYCMD, "| $cmd") || die qq(Can't open pipe to "$cmd"!\n);
print MYCMD "yourresponsehere\n";
HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top