tsathish123 has asked for the wisdom of the Perl Monks concerning the following question:

Below is my question

I start the process in Linux. Give input through terminal or command window to the process to do different set of actions.

I would like to automate the same by using perl script.

Like start the particular process. After process started, I would like to give input to the process like i give through keyboard to terminal.

Since i am a beginner to perl language. Please help me by giving ideas or example code snippet.

  • Comment on How to give input to terminal (command window) in perl

Replies are listed 'Best First'.
Re: How to give input to terminal (command window) in perl
by Discipulus (Canon) on Aug 25, 2016 at 06:55 UTC
    Hello and welcome to the monastery tsathish123

    as said Expect (Edit: and Test::Expect too) can be an option. Wath you want is generally called IPC (Inter Process Communication) ad the Perl way is described in the language reference found here: perlipc

    While two core modules can accomplish your task ( IPC::Open2 and IPC::Open3 which also manage STDERR ) on CPAN exist many other options, sometimes more viable like IPC::Run IPC::Run3 or if you just need to read from external program also Capture::Tiny

    The old but always recomandable book Perl Cookbook contains a chapter, the 16th, dedicated to Process Management and Communication

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to give input to terminal (command window) in perl
by kcott (Archbishop) on Aug 25, 2016 at 06:36 UTC

    G'day tsathish123,

    Welcome to the Monastery.

    That sounds like a job for Expect, whose abstract reads:

    "automate interactions with command line programs that expose a text terminal interface"

    Depending on how much of a "beginner" you are, you might want to read "perlintro -- a brief introduction and overview of Perl" first. Each section of this has multiple links to further details and more advanced topics: follow as required.

    — Ken

Re: How to give input to terminal (command window) in perl
by Marshall (Canon) on Aug 25, 2016 at 19:51 UTC
    It would be helpful if you explained your situation in some more detail. If there is just a fixed set of stuff that you are going to type into this program without caring what the prompts that it says are, then program <cmds.txt will often do the job. You could use Perl to make a shell script that potentially could do what you need. The most complicated version of this is having a Perl program "act like you would act" and respond differently to different prompts. Without more information, I am hesitant to recommend that approach because easier solutions might work for you.