0

I am trying to execute this in my perl script

my $command = `ps -p  $pidnumber |  wc -l`;

but when I run the script it show me something like this:

sh: -c: line 1: syntax error near unexpected token `|'

sh: -c: line 1: ` | wc -l'

Which would it may to be the error?

1
  • 8
    Where is $pidnumber coming from? Might it have a newline appended to it? Commented Apr 2, 2013 at 17:13

3 Answers 3

1

Does $pidnumber end with a new line? See the chomp function.

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

Comments

1

try print before execute the command, because $pidnumber may have something strange:

my $command_str = "ps -p  $pidnumber |  wc -l";
print $command_str, "\n";
my $command = qx!$command_str!;

as @mob said, use chomp if newline appears in the print sentence.

1 Comment

use Data::Dumper; local $Data::Dumper::Useqq = 1; print(Dumper($command_str)); for more useful at finding oddities, especially trailing newlines.
1

I agree with the other answers that an extra newline before the pipe is quite possibly the cause

But the command doesn't really need the pipe!

force the quoted command to return it's value as a list and then get the length of the list and you've got the wc -l "line count"

my $command_str = "ps -p  $pidnumber";
my $linecount = () = qx!$command_str!;

Comments

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.