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

Hi Monks,

Seek your help again

In a small perl script I am using here document to execute multipe commands and store the obtained output in an array.

Below is my code

@result=`sh /tmp/bin/ssmcons 192.168.2.114 << EOF; access; subagent list; quit; EOF`;

With this code I am trying to run a binary and execute some commands related to that.

The script is getting stuck without any output. When I observe the ps output it is giving me

root 6513 6118 0 18:41 pts/4 00:00:00 perl ssm_conf.pl root 6514 6513 0 18:41 pts/4 00:00:00 sh -c sh /tmp/bin/ssmc +ons 192.168.2.114 << EOF;?access;?subagent list;?quit;?EOF
Can you help me in overcomming this problem

Thanks in Advance.

Srinivas.

Replies are listed 'Best First'.
Re: here document help
by Fletch (Bishop) on Apr 03, 2008 at 13:43 UTC

    Rather than trying to do shell redirection trickery inside your backticks, perhaps use something like IPC::Open2 or IPC::Run to open reader and writer pipes and send the data to the process' stdin that way.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      We have tried that method also. Since we are running this in threads, the above solution did not fetch proper results.

      Is there any solution other than this?

Re: here document help
by superfrink (Curate) on Apr 03, 2008 at 18:34 UTC
    First of all (just so you know) the code is using a Bourne Shell heredoc. Not a perl heredoc.

    I don't see any problem with the code. I ran the following (on Fedora 6):
    #! /usr/bin/perl -w use strict; use Data::Dumper; my @result = `sh << EOF; pwd; sleep 10; date; EOF`; print Dumper(\@result);
    The output looked fine:
    $ ./678154.pl $VAR1 = [ '/home/chad ', 'Thu Apr 3 12:27:32 MDT 2008 ' ];
    The ps output looks fine.
    6204 pts/1 S+ 0:00 sh -c sh << EOF;?pwd;?sleep 10;?date;?EOF
    Have you tried running the command from a shell yourself? I suspect the ssmcons program is hanging. Maybe 192.168.2.114 is not responding or closing the connection.
      Thanks for the reply

      I tried to execute the command in the command prompt with an <Enter> after each statement and it is giving me the correct output with out hanging.

      since I am new to this perl programming..can you tell me whether a script working from command prompt will hang when executed from perl script?

      waiting for your valuable suggestions.

      Srinivas.

        Launching a shell interactively and manually entering the script line by line is different from launching a shell with a here-document.

        I recommend that you either launch the shell with the commands to execute separated by semicolons or that you write out a shell script and launch that shell script from Perl. Both ways are far easier to debug.

        Also, I'm not really sure whether your command line is correct. You should launch the shell with sh -c commands... and not sh comands... I think. But then, I avoid such trickery.