如何在perl中调用unix

时间:2015-01-07 10:45:59

标签: perl unix

我正在尝试执行以下命令

#!/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";

但是收到错误:

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

请帮助我如何在perl脚本中调用unix命令

1 个答案:

答案 0 :(得分:4)

如果第16行是拼写错误,而不是

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

你应该写

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

如果它不是拼写错误,请写下:

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

"双引号"每个包含空格/元字符和每个扩展的文字:"$var""$(command "$var")""${array[@]}""a & b"。使用'single quotes'代码或文字$'s: 'Costs $5 US'ssh host 'echo "$HOSTNAME"'。见
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words