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

I am trying to write a script that will read stdin (from a program that sends messages to the command line) and then have my script loop through each message as it appears and answer the message with a Y. Once the messages cease I would like my script to continue. I include the code below only to show that I have tried some stuff and am not asking for someone to do all the coding for me, I'm just stumped at trying to get the thing to redo its loop and then stop when needed. THanks in Advance! Bob
#!/usr/bin/perl 'composer "create originaljobfile from jobs=@#@"'; 'composer "delete jobs=ESMD#@"'; chomp ($input= <STDIN>); if ($input=~ /^Okay\b/i) { print "Y"; redo; } else { print "joe"; };

Replies are listed 'Best First'.
Re: trying to get a loop to redo
by dws (Chancellor) on Jul 17, 2002 at 18:07 UTC
    Once the messages cease I would like my script to continue.

    Simple. Do something like

    while ( <STDIN> ) { chomp($input = $_); if ( $input =~ /^okay\b/i ) { print "Y" } } # script continues here when STDIN is exhausted
Re: trying to get a loop to redo
by VSarkiss (Monsignor) on Jul 17, 2002 at 18:42 UTC

    dws has put you on the right track, but I'm having a little trouble with this statement:

    ...read stdin (from a program that sends messages to the command line)...
    I'm not sure I understand what you mean by "messages to the command line". However, your code snippet leads me to believe you're trying to invoke a program called composer from within your Perl program, then have it reply semi-intelligently, similar to an expect script.

    To do that requires more work than just reading STDIN and writing STDOUT. You might want to look up IPC::Open2, IPC::Open3, or, if you really, really want to roll your own, the pipe function. Look at the perlipc docs for more detail.

    I don't want to elaborate any further since I'm not sure I'm answering the question you intended to ask...

Re: trying to get a loop to redo
by ronzomckelvey (Acolyte) on Jul 21, 2002 at 05:22 UTC
    If your just running a script or program and need 'y' piped to it, in linux you can use yes. Yes outputs Y <cr>
    composer "yes | create originaljobfile from jobs=@#@"';