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

my $ex = QA::STK::Execute->new(ip=>'some ip',user=>'root'); $ex->command("ls-l"); my $rc = $ex->execute(); my $output = $ex->output();
if want to check whether xyz file in present in " ls-l" , how to do that

Replies are listed 'Best First'.
Re: perl script
by Corion (Patriarch) on Jan 12, 2017 at 13:26 UTC

    What value is in $output?

    You can inspect the value in $output by doing:

    print $output;

    See index.

      print $output; my $ex = QA::STK::Execute->new(ip=>'some ip',user=>'root'); $ex->command("ls-l"); my $rc = $ex->execute(); my $output = $ex->output(); Gives perl doubt.

        It may be useful to print the value of  $output after it has been given some (possibly) meaningful value:

        ... my $output = $ex->output(); print ">>$output<<"; ...
        This may remove doubt. Note that the variable is being printed with clear indications of its start and end.


        Give a man a fish:  <%-{-{-{-<

Re: perl script
by kennethk (Abbot) on Jan 12, 2017 at 19:35 UTC
    First, please read How do I post a question effectively?. In particular, please wrap code in <code> tags so that we can be sure your content doesn't get mangled. I'll be working with
    my $ex = QA::STK::Execute->new(ip=>'some ip',user=>'root'); $ex->command("ls-l"); my $rc = $ex->execute(); my $output = $ex->output();
    I cannot find any reference to a module called QA::STK::Execute, but I will make assumptions based on syntax. If you review the Basic debugging checklist, the first thing you should check is does $output contain what you think it does. printing is the easiest basic check. Given that you are running ls-l in your posted code and not ls -l (note the space), I'd bet you've got empty output.

    Once you've verified that it seems reasonable, you can either use index as Corion suggested, or craft a regular expression. A regular expression has the advantage of anchoring to the end of a line, since you are looking for a file name. Maybe something like:

    if (/\s\Q$xyz\E$/) { # Code to run }
    The easiest way to deal with this is for you to post some example output from ls -l and then what you tried in order to find the file's name, all wrapped in <code> tags.

    You will note that your lack of adherence to site expectations has garnered you down votes. Read the links, and see if they make sense. You can even edit your posts to fix them up.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      print index($output); my $ex = QA::STK::Execute->new(ip=>'some ip',user=>'root'); $ex->command("ls-l"); my $rc = $ex->execute(); my $output = $ex->output();
        What do you expect this code to do?
        1. index takes two arguments, and you have not indicated what to look for. What you've posted is a compile time syntax error.
        2. $output cannot have any value until after you assign it, so the print statement would need to be at the end.
        3. You did not follow my previous direction to wrap code in code tags.
        4. You still don't have a space in ls -l
        5. Your code will not pass strict with your placement of your print statement. See Use strict warnings and diagnostics or die.
        What do you get when you run:
        my $ex = QA::STK::Execute->new(ip=>'some ip',user=>'root'); $ex->command("ls-l"); my $rc = $ex->execute(); my $output = $ex->output(); print $output;
        Does your module have a capability to read STDERR?

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.