2

Im trying to code a "service" script based on "ps". my code:

#!/usr/bin/perl
use strict;
use warnings;
die "usage:    $0 <service name>\n" unless $ARGV[0];
my $service = $ARGV[0];
open(my $ps, "ps -aux |") || die "Uknown command\n";
my @A = <$ps>;
close $ps;
foreach my $i(grep /$service/, @A){
    chomp $i;
    if($i=~ /root/){
        next
    }
    print "$i\n";
}

My problem: When running the script against undef arg like:

$0 blablabla 

I want to return an output if there is no such service appears/when returns 0 Thanks

4 Answers 4

2

I assume what you are asking is: How to give a proper message when no matching lines are found?

Well, just store the result in an array instead:

my @lines = grep { !/root/ && /$service/ } @A;

if (@lines) {   # if any lines are found
    for my $line (@lines) {
        ...
    }
} else {
    print "No match for '$service'!\n";
}

Or you can print the number of matches regardless of their number:

my $found = @lines;
print "Matched found: $found\n";

Note also that you can add the check for root in your grep.

As a side note, this part:

die "usage:    $0 <service name>\n" unless $ARGV[0];
my $service = $ARGV[0];

Is perhaps better written

my $service = shift;
die "usage ...." unless defined $service;

Which specifically checks if the argument is defined or not, as opposed to true or not.

Sign up to request clarification or add additional context in comments.

Comments

2

If I understand you correctly, you want to inform the user if no such service was found? If so, you can modify the script as follows:

my $printed;                        # Will be used as a flag.
foreach my $i(grep /$service/, @A){
    chomp $i;
    if($i=~ /root/){
        next
    }
    $printed = print "$i\n";        # Set the flag if the service was found.
}
warn "No service found\n" unless $printed;

Comments

1

You can try something like this:

my @processes = grep /$service/, @A;
if ( scalar @processes ) {
    foreach my $i( @processes ){
        chomp $i;
        if($i=~ /root/){
            next;
        }
        print "$i\n";
    }
}
else {
    print 'your message';
}

Comments

0

You could check the result of the grep command before traversing it in the for loop, like:

...

my @services = grep { m/$service/ } @A;

# Filter the perl process running this script and...
if ( ! @services ) { 
    print "No service found\n";
    exit 0;
}

foreach my $i( @services ){
    ...
}

Take into account that the grep command will never give a false return because it is including the perl process, so you will have to filter it, but I hope you get the idea.

1 Comment

I got it. This is actually the logical way, however my code [not posted here] didn't work. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.