Amit
Amit

Reputation: 43

how to invoke unix inside perl

i am trying to execute 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 an 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 invoke unix command inside perl script

Upvotes: 3

Views: 147

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185560

If line 16 is a typo, instead of

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

you should wrote

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

If it's not a typo, then write :

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

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for 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

Upvotes: 4

Related Questions