Reputation: 2431
I am trying to execute the following unix command from within perl
`git log --pretty=format:%H | grep $id | wc -l`;
I keep getting an error saying
sh: -c : syntax error near unexpected token '|'
sh -c : '| wc -l'
How can I fix this error
Upvotes: 0
Views: 377
Reputation: 29625
You'll probably have a more robust script if you implement some of this functionality using Perl subroutines instead of external Unix commands.
Using backquotes is good for a quick script, but it's inherently fragile, because you are exposed to the full power and weirdness of whatever Unix shell is configured for the user that is running your script (with wildcard expansion, input/output redirection, piping, etc.). This really applies to any programming language, and most languages (including perl) have safer ways to run external commands without using the shell. But one of the reasons Perl was created was to be able to combine under one programming language the text-processing abilities of a slew of Unix commands.
Upvotes: 1
Reputation: 385847
The problem is that you have 123<NEWLINE>
instead of 123
in $id
, so you're passing the following to the shell:
git log --pretty=format:%H | grep 123
| wc -l
instead of
git log --pretty=format:%H | grep 123 | wc -l
Fix the value in $id
. The best way is probably through chomp
.
Upvotes: 5