0

这是我的 perl 脚本代码。在这我收到错误,例如“在 $cmd 操作员预期的位置找到裸字”

my $path = $folder."/".$host."_".$database_name.".bak";
$cmd .= "-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" ";

有人帮我吗?

4

2 回答 2

4

当字符串中包含双引号时,您需要使用\.

$cmd .= "-U $user_name -P $password -S $host -d master -Q \"BACKUP DATABASE [$database_name] TO DISK = N'$path'\" ";

此外,Perl 允许您使用其他字符作为引号分隔符。qq后跟几乎任何字符都与双引号相同。所以你可以做这样的事情来避免反斜杠的需要:

$cmd .= qq(-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" );

$cmd .= qq|-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" |;

等等...

更新:如何在 Perl 中执行系统命令。有以下三种基本方式:

system($cmd);  #Goes through the shell if shell metacharacters are detected.
system(@command_and_args);  #first element is the command, the rest are arguments

system executes a command and waits for it to return. The return value is the exit status of the program.

my @results = `$cmd`;  #Always goes through shell.

Backticks execute a command and return its output. You should only use this if you actually need the output; otherwise, it is better to go with system.

exec $cmd;
exec @command_and_args;

exec is exactly like system, except that it never returns. It effectively ends your program by calling another program.

Use the one that is most appropriate to your situation. Or in this case, since you are executing SQL, consider using the DBI module. It's definitely a better approach for anything more than a couple of simple commands.

于 2012-10-22T07:39:06.140 回答
1

看起来你的"角色放错了地方。我不确定他们应该在哪里。

在第二行中,字符串文字:

"-U $user_name -P $password -S $host -d master -Q "

紧随其后的是裸词

BACKUP
于 2012-10-22T07:25:15.053 回答