php 将字符串传递给外部 diff 命令

发布于 2025-01-03 10:45:38 字数 191 浏览 1 评论 0原文

我的程序生成两个字符串,我希望通过外部 diff 工具对它们进行比较。 diff 工具仅接受文件/目录作为参数。这是 diff file1 file2 工作正常,但 diff "hello" "world" 不起作用。有没有办法将我的字符串直接传递给 diff 而不创建任何临时文件?谢谢。

My program generates two strings and I want them compared by the external diff tool. The diff tool accepts only files/directories as arguments. That's diff file1 file2 works perfectly but diff "hello" "world" doesn't work. Is there a way to pass my strings directly to diff without creating any temporary files? Thanks.

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

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

发布评论

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

评论(1

反话 2025-01-10 10:45:38

在外壳上,您可以使用临时管道。

diff <(echo "string 1") <(echo "string 2")

使用反引号运算符或任何其他方法在 php.ini 中执行命令。有关执行命令的详细信息,请参阅手册: http://www.php.net /manual/en/ref.exec.php

确保正确转义字符串。

编辑:此功能称为临时管道。因此 shell 将其转换为文件描述符。

iblue@nerdpol:~$ echo <(echo "string")
/dev/fd/63
iblue@nerdpol:~$ cat <(echo "string")
string

有关详细说明,请参阅 http://www.linuxjournal.com/article/2156?page=0,1" rel="nofollow">http://www.linuxjournal.com/article/2156?页=0,1

On the shell, you can use temporary pipes.

diff <(echo "string 1") <(echo "string 2")

Use the backticks operator or any other method to execute the command in php. For details on executing commands, see the manual: http://www.php.net/manual/en/ref.exec.php

Make sure, you properly escape the strings.

EDIT: This feature is called temporary pipes. So the shell translates it to a file descriptor.

iblue@nerdpol:~$ echo <(echo "string")
/dev/fd/63
iblue@nerdpol:~$ cat <(echo "string")
string

For a detailed explanation see http://www.linuxjournal.com/article/2156?page=0,1

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