FAQ
hi,

i need to print the output of a command on the console at runtime
lets say, i need to execute find command .as of now , what i am doing is ,

@cmd= `find . -name "abc"`;
print "@cmd\n";

now what happens is, once the command completed then it will send entire output to @cmd
and then entire output gets printed to console in one shot

instead of that , i need output to be printed as it progresses


plz suggest


regards
irfan

Search Discussions

  • Chandrashekar Bhat at Jul 13, 2011 at 6:14 am
    You can use system command inside perl

    system(" find . -iname 'abc'");

    --
    Shekar
    On Wed, Jul 13, 2011 at 11:16 AM, Irfan Sayed wrote:

    hi,

    i need to print the output of a command on the console at runtime
    lets say, i need to execute find command .as of now , what i am doing is ,

    @cmd= `find . -name "abc"`;
    print "@cmd\n";

    now what happens is, once the command completed then it will send entire
    output to @cmd
    and then entire output gets printed to console in one shot

    instead of that , i need output to be printed as it progresses


    plz suggest


    regards
    irfan
  • Jim Gibson at Jul 13, 2011 at 6:28 am

    At 10:46 PM -0700 7/12/11, Irfan Sayed wrote:
    hi,

    i need to print the output of a command on the console at runtime
    lets say, i need to execute find command .as of now , what i am doing is ,

    @cmd= `find . -name "abc"`;
    print "@cmd\n";

    now what happens is, once the command completed then it will send
    entire output to @cmd
    and then entire output gets printed to console in one shot

    instead of that , i need output to be printed as it progresses
    You have two choices that I can think of:

    1. Use the Perl module File::Find instead of forking an external
    process to run the operating system's find command.

    See 'perldoc File::Find'.

    Example (untested):

    use File::Find;
    find( sub{
    return unless $_ eq 'abc';
    print qq($File::Find::name\n)
    }, q(.)
    );

    2. Fork the find program using open and a mode parameter of '-|'
    instead of back-quotes.

    See 'perldoc -f open'.

    Example (untested):

    open( my $find, '-|', q(find . -name "abc")) or die("Can't fork find
    program: $!");
    while( <$find> ) {
    print;
    }

    --
    Jim Gibson
    Jim@Gibson.org
  • John W. Krahn at Jul 13, 2011 at 6:53 am

    Irfan Sayed wrote:
    hi,
    Hello,

    i need to print the output of a command on the console at runtime
    lets say, i need to execute find command .as of now , what i am doing is ,

    @cmd= `find . -name "abc"`;
    print "@cmd\n";

    now what happens is, once the command completed then it will send entire output to @cmd
    and then entire output gets printed to console in one shot

    instead of that , i need output to be printed as it progresses
    open my $PIPE, '-|', 'find', ',', '-name', 'abc' or die "Cannot open
    pipe from 'find' because: $!:

    while ( <$PIPE> ) {
    print;
    }

    close $PIPE or warn $! ? "Error closing 'find' pipe: $!"
    : "Exit status $? from 'find'";



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
  • Irfan Sayed at Jul 13, 2011 at 7:28 am
    thanks John and Jim
    but, is this solution is applicable to only "find" command ?
    if i change the command to some other system command , will this solution work?

    plz suggest



    ________________________________
    From: Jim Gibson <jimsgibson@gmail.com>
    To: Perl Beginners <beginners@perl.org>
    Sent: Wednesday, July 13, 2011 12:00 PM
    Subject: Re: print output on console at runtime




    You chose to allow Jim Gibson (jimsgibson@gmail.com) even though this message failed authentication
    Click to disallow
  • Jim Gibson at Jul 13, 2011 at 2:45 pm

    At 12:27 AM -0700 7/13/11, Irfan Sayed wrote:
    thanks John and Jim
    but, is this solution is applicable to only "find" command ? No.
    if i change the command to some other system command , will this
    solution work?
    Yes.
  • Irfan Sayed at Jul 28, 2011 at 6:36 am
    can this be happen if command needs to be executed on remote machine and the output needs to be forked on the local console at runtime


    please suggest


    regards
    irfan



    ________________________________
    From: Jim Gibson <jimsgibson@gmail.com>
    To: Perl Beginners <beginners@perl.org>
    Sent: Wednesday, July 13, 2011 12:00 PM
    Subject: Re: print output on console at runtime




    You chose to allow Jim Gibson (jimsgibson@gmail.com) even though this message failed authentication
    Click to disallow
  • Irfan Sayed at Jul 28, 2011 at 2:05 pm
    further,

    i am executing following command using open

    open $frk, "devenv /rebuild release ReusableU.sln /useenv 2>&1 |" or die "Couldn't execute program: $!";
    now i need to capture the exit status of this command

    i tried with $? but it does not contain the exit status

    please suggest


    regards
    irfan



    ________________________________
    From: Irfan Sayed <irfan_sayed2002@yahoo.com>
    To: Jim Gibson <jimsgibson@gmail.com>; Perl Beginners <beginners@perl.org>
    Sent: Thursday, July 28, 2011 12:06 PM
    Subject: Re: print output on console at runtime


    can this be happen if command needs to be executed on remote machine and the output needs to be forked on the local console at runtime


    please suggest


    regards
    irfan



    ________________________________
    From: Jim Gibson <jimsgibson@gmail.com>
    To: Perl Beginners <beginners@perl.org>
    Sent: Wednesday, July 13, 2011 12:00 PM
    Subject: Re: print output on console at runtime




    You chose to allow Jim Gibson (jimsgibson@gmail.com) even though this message failed authentication
    Click to disallow

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 13, '11 at 5:46a
activeJul 28, '11 at 2:05p
posts8
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase