How to invoke unix inside perl

I am trying to execute the following command

#!/usr/bin/perl
$num_args = $#ARGV + 1;
if ($num_args != 1) {
     print "\nUsage: name.pl srv_name \n";
     exit;
}

$srv_name=$ARGV[0];
#$last_name=$ARGV[1];

if( $srv_name eq "afs" || $srv_name eq "mnp") {
    print "You have entred Service Name=$srv_name\n";
}

$cmd= `sepman -l|grep -e $srv_name\( | wc -l`;

print "cmd= $cmd\n";

      

But getting error:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `sepman -l|grep -e afs( | wc -l'

      

please help me how to call unix command inside perl script

+3


source to share


1 answer


If line 16 is a typo and not

$cmd= `sepman -l|grep -e $srv_name\( | wc -l`;

      

you have to write

$cmd= `sepman -l|grep -e "$srv_name" | wc -l`;

      



If it's not a typo, write:

$cmd= `sepman -l|grep -e "$srv_name\(" | wc -l`;

      

"Double quote" every literal that contains spaces / meta characters and each decomposition: "$var"

, "$(command "$var")"

, "${array[@]}"

, "a & b"

. Use 'single quotes'

a code or literal $'s: 'Costs $5 US'

, ssh host 'echo "$HOSTNAME"'

. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

+4


source







All Articles