为什么这个命令不获取两个目录的差异?

发布于 2024-09-30 22:25:48 字数 188 浏览 5 评论 0原文

我被要求使用 Perl 比较两个目录,但我认为我的命令有问题,

$diff = system("sudo diff -r '/Volumes/$vol1' '/Volumes/$vol2\\ 1/' >> $diff.txt");

它不显示和输出。有人可以帮我解决这个问题吗?谢谢!

I am asked to diff two directories using Perl but I think something is wrong with my command,

$diff = system("sudo diff -r '/Volumes/$vol1' '/Volumes/$vol2\\ 1/' >> $diff.txt");

It doesn't display and output. Can someone help me with this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉城凉梦凉人心 2024-10-07 22:25:48

看来您想将所有差异存储在一个字符串中。

如果是这种情况,问题中的命令将因以下几个原因而无法工作:

  • 很难判断它是否有意为之,但 $diff 变量被用于设置存储差异的文件名。也许这应该是 diff.txt,而不是 $diff.txt

  • diff 命令的结果保存在 $ 中diff.txt。它不会在 STDOUT 中显示任何内容。这可以通过省略 >> 来解决。 $diff.txt 部分。如果还需要存储在文件中,请考虑使用 tee 命令:

    sudo diff -r dir1/dir2/|三通差异.txt
    
  • 系统调用被赋值给一个变量,成功时返回0。引用文档:

    <块引用>

    返回值是wait返回的程序的退出状态调用。


    这意味着 $diff 不会存储差异,而是存储命令退出状态。更明智的方法是使用反引号。这样做将允许 $diff 存储命令输出到 STDOUT 的任何内容:

    my $diff = `sudo diff -r dir1/ dir2/ | tee diff.txt`; # 不是 $diff.txt
    

  • 是否必须使用 sudo 命令?如果可能的话,请避免使用它:

    my $diff = `diff -r dir1/ dir2/ | tee diff.txt`; # 不是 $diff.txt
    

最终建议

让一个好的 CPAN 模块来处理这项任务,因为反引号调用只能走这么远。这里已经建议了一些;它可能非常值得一看。

It seems that you want to store all differences in a string.

If this is the case, the command in the question is not going to work for a few reasons:

  • It's hard to tell whether it's intended or not, but the $diff variable is being used to set the filename storing the differences. Perhaps this should be diff.txt, not $diff.txt

  • The result of the diff command is saved in $diff.txt. It doesn't display anything in STDOUT. This can be remedied by omitting the >> $diff.txt part. If it also needs to be stored in file, consider the tee command:

    sudo diff -r dir1/ dir2/ | tee diff.txt
    
  • When a system call is assigned to a variable, it will return 0 upon success. To quote the documentation:

    The return value is the exit status of the program as returned by the wait call.

    This means that $diff won't store the differences, but the command exit status. A more sensible approach would be to use backticks. Doing this will allow $diff to store whatever is output to STDOUT by the command:

    my $diff = `sudo diff -r dir1/ dir2/ | tee diff.txt`;  # Not $diff.txt
    
  • Is it a must to use the sudo command? Avoid using it if even remotely possible:

    my $diff = `diff -r dir1/ dir2/ | tee diff.txt`;  # Not $diff.txt
    

A final recommendation

Let a good CPAN module take care of this task, as backtick calls can only go so far. Some have already been suggested here; it may be well worth a look.

江心雾 2024-10-07 22:25:48

是否提示 sudo diff 输入密码?
如果可能,从 diff 调用中取出 sudo,并使用 sudo 运行脚本。

Is sudo diff being prompted for a password?
If possible, take out the sudo from the invocation of diff, and run your script with sudo.

一杯敬自由 2024-10-07 22:25:48

“它不显示和输出。” -- 这是因为您将差异保存到文件中,然后(大概)不对生成的文件执行任何操作。

但是,我希望“使用 Perl 比较两个目录”并不意味着“使用 system() 在 shell 中执行此操作,然后捕获结果”。您是否考虑过用语言本身来做到这一点?例如,请参阅 Text::Diff。为了更细致地控制“差异”的构成,您可以简单地读取每个文件并编写自己的算法来执行比较并编译相似点和差异。

"It doesn't display and output." -- this is becuase you are saving the differences to a file, and then (presumably) not doing anything with that resulting file.

However, I expect "diff two directories using Perl" does not mean "use system() to do it in the shell and then capture the results". Have you considered doing this in the language itself? For example, see Text::Diff. For more nuanced control over what constitutes a "difference", you can simply read in each file and craft your own algorithm to perform the comparisons and compile the similarities and differences.

冷情 2024-10-07 22:25:48

您可能想查看 Test::Differences以获得更灵活的 diff 实现。

You might want to check out Test::Differences for a more flexible diff implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文