为什么这个命令不获取两个目录的差异?
我被要求使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您想将所有差异存储在一个字符串中。
如果是这种情况,问题中的命令将因以下几个原因而无法工作:
很难判断它是否有意为之,但
$diff
变量被用于设置存储差异的文件名。也许这应该是diff.txt
,而不是$diff.txt
diff
命令的结果保存在$ 中diff.txt
。它不会在 STDOUT 中显示任何内容。这可以通过省略>> 来解决。 $diff.txt
部分。如果还需要存储在文件中,请考虑使用tee
命令:当
系统
调用被赋值给一个变量,成功时返回0。引用文档:<块引用>
返回值是
wait返回的程序的退出状态
调用。
这意味着
$diff
不会存储差异,而是存储命令退出状态。更明智的方法是使用反引号。这样做将允许$diff
存储命令输出到 STDOUT 的任何内容:是否必须使用
sudo
命令?如果可能的话,请避免使用它:最终建议
让一个好的 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 bediff.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 thetee
command:When a
system
call is assigned to a variable, it will return 0 upon success. To quote the documentation: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:Is it a must to use the
sudo
command? Avoid using it if even remotely possible: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.
是否提示 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.
“它不显示和输出。” -- 这是因为您将差异保存到文件中,然后(大概)不对生成的文件执行任何操作。
但是,我希望“使用 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.您可能想查看
Test::Differences
以获得更灵活的 diff 实现。You might want to check out
Test::Differences
for a more flexible diff implementation.