FAQ
Hi,
I'm trying to use the output of a grep command in a shell script.
If I do-
$cmd = "grep 'abc' ./*";
$status = system $cmd;
Then $status always turns out to be 0, even if I use invalid arguments with
grep.
If the result of grep is not empty, I want to perform some action and if it
is empty, I want to do something else.
How can I get the output of grep into a string variable in the Perl script?
Any help will be highly appreciated.
Thanks.

Neelay.

Search Discussions

  • Riehm Stephen at Sep 9, 2005 at 9:42 am
    try:

    @grep_results = qx{ grep 'abc' ./* };
    $grep_status = $?;

    system is only really usefull for starting programs which then do their own
    thing. If you want the output, use qx!

    Cheers,

    Steve
    On 08.09.2005 23:42 Uhr, "neelay thaker" wrote:

    Hi,
    I'm trying to use the output of a grep command in a shell script.
    If I do-
    $cmd = "grep 'abc' ./*";
    $status = system $cmd;
    Then $status always turns out to be 0, even if I use invalid arguments with
    grep.
    If the result of grep is not empty, I want to perform some action and if it is
    empty, I want to do something else.
    How can I get the output of grep into a string variable in the Perl script?
    Any help will be highly appreciated.
    Thanks.

    Neelay.


    Der Inhalt dieser E-Mail oder eventueller Anhange ist ausschliesslich
    fur den bezeichneten Adressaten bestimmt. Wenn Sie nicht der
    vorgesehene Adressat dieser E-Mail oder dessen Vertreter sein
    sollten, so beachten Sie bitte, dass jede Form der Kenntnisnahme,
    Veroffentlichung, Vervielfaltigung oder Weitergabe des Inhalts
    dieser E-Mail unzulassig ist. Wir bitten Sie, sich in diesem Fall
    mit dem Absender der E-Mail in Verbindung zu setzen.

    The information contained in this email is intended solely for
    the addressee. If you are not the intended recipient, any form
    of disclosure, reproduction, distribution or any action taken
    or refrained from in reliance on it, is prohibited and may
    be unlawful. Please notify the sender immediately..
  • Jonas B . Nielsen at Sep 9, 2005 at 10:26 am
    Hello,

    If you want to catch the output of a command, use:

    $data = qx/$cmd/

    The status you where getting back when calling system indicated success
    of your execution with 0.

    The return value of 0 is true for shell (mostly), it is platform
    dependent I suppose, so you have to turn you Perl logic around.

    Often see Perl scripts end with exit(0); for communication with the
    shell executing the perl script in question.

    jonasbn
    On 8/9-2005, at 23.42, neelay thaker wrote:

    Hi,
    I'm trying to use the output of a grep command in a shell script.
    If I do-
    $cmd = "grep 'abc' ./*";
    $status = system $cmd;
    Then $status always turns out to be 0, even if I use invalid
    arguments with grep.
    If the result of grep is not empty, I want to perform some action and
    if it is empty, I want to do something else.
    How can I get the output of grep into a string variable in the Perl
    script?
    Any help will be highly appreciated.
    Thanks.

    Neelay.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupscripts @
categoriesperl
postedSep 8, '05 at 9:50p
activeSep 9, '05 at 10:26a
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase