简体   繁体   English

为什么我的perl系统命令不起作用?

[英]why is my perl system command not working?

This is my perl code to move result to the sent folder. 这是我的perl代码,可将结果移动到已发送的文件夹中。

 system("mv /home/pi/downloads/$result /home/pi/downloads/sent/$result");

The error i get is: 我得到的错误是:

mv: missing destination file operand after `/home/pi/downloads/filename.txt'

What am i doing wrong here? 我在这里做错了什么?

Most likely $result contains a newline, which prematurely terminates the command. $result最有可能包含换行符,该换行符会提前终止命令。 Use chomp to discard the extra newline. 使用chomp丢弃多余的换行符。

If $result comes from user input and it wasn't chomped, there's almost certainly a newline character. 如果$result来自用户输入并且没有被限制,那么几乎可以肯定会有换行符。 And, depending on the audience for your program, you now have a malicious code injection problem. 而且,根据您的程序的受众,您现在遇到了恶意代码注入问题。

To avoid the injection problem, how about using the rename function to move the file into the destination? 为避免注入问题,如何使用rename功能将文件移至目标位置?

That's difficult to say but there's a few things you can do that should help you solve it. 很难说,但是您可以做一些事情来帮助您解决问题。

First make sure your script starts like this: 首先,请确保您的脚本是这样启动的:

#!/pathe/to/perl -w

use strict;

Notice the -w to enable warnings. 注意-w以启用警告。 Also use strict will help you identify code issues. 也可以use strict来帮助您识别代码问题。

Another thing that helps a lot is storing the command you want to run in a scalar and printing it to see what it is actually doing: 有用的另一件事是将要运行的命令存储在标量中并打印出来以查看其实际作用:

my $result = "filename.txt";
chomp($result);
my $cmd = sprintf("mv /home/pi/downloads/%s /home/pi/downloads/sent/%s", $result, $result);
print "$cmd\n";
system($cmd);

In your script do you get the value for $result from user input? 在您的脚本中,您是否从用户输入中获得了$ result的值? I have a feeling it has a newline character. 我觉得它具有换行符。 The chomp function will safely remove newline characters from the end of a string. chomp函数将安全地从字符串末尾删除换行符。

While you can call external programs to do something for you using system() or qx{} , Perl is extremely powerful and versatile, and for many common operations it can be done using just Perl itself (or its numerous modules), without using anything external. 尽管您可以使用system()qx{}调用外部程序来为您做某事,但是Perl极其强大且用途广泛,对于许多常见操作,可以仅使用Perl本身(或其众多模块)完成,而无需使用任何功能外部。 It is both faster and more reliable if any problems with external programs arise. 如果外部程序出现任何问题,它将更快,更可靠。 For example, if external executable is buggy and spins in tight loop, system() may never return, freezing your script. 例如,如果外部可执行文件有错误,并且循环紧密,则system()可能永远不会返回,从而冻结脚本。

In your case, this Perl code works better AND handles errors: 在您的情况下,此Perl代码可更好地工作并处理错误:

use File::Copy;

unless (move ("/home/pi/downloads/$result",
              "/home/pi/downloads/sent/$result"))
{
    print "Rename has failed!\n";
    # ...
}

(Of course, you should make sure that $result does not contain newlines before you run this.) (当然,在运行此命令之前,应确保$result不包含换行符。)

Check both $result and the directories themselves: 检查$result和目录本身:

  • Make sure $result doesn't have slashes in it (unless you're expecting subdirectories) 确保$result中没有斜杠 (除非您期望有子目录)
  • Make sure $result doesn't have spaces in it since the command doesn't use quoting 确保$result中没有空格 ,因为该命令不使用引号
  • Make sure $result isn't empty 确保$result不为空

For the directories: 对于目录:

  • Make sure /home/pi/downloads exists and is a directory 确保/home/pi/downloads存在并且是目录
  • Make sure /home/pi/downloads/sent exists and is a directory 确保/home/pi/downloads/sent存在并且是目录
  • Make sure /home/pi/downloads has writable permissions 确保/home/pi/downloads具有可写权限
  • Make sure /home/pi/downloads/sent has writable permissions 确保/home/pi/downloads/sent具有可写权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM