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.
-
What is "the external diff tool"?Richard– Richard2012-02-08 13:48:11 +00:00Commented Feb 8, 2012 at 13:48
-
what prograsm you are using for diff ? (more details please)bizzr3– bizzr32012-02-08 13:48:52 +00:00Commented Feb 8, 2012 at 13:48
-
The linux diff tool. Simply $> diffShawn– Shawn2012-02-08 13:50:10 +00:00Commented Feb 8, 2012 at 13:50
-
And what's wrong with PHP string functions to get the diff?anubhava– anubhava2012-02-08 13:51:30 +00:00Commented Feb 8, 2012 at 13:51
-
In my case, the strings are probably multi-line. I found diff's -y options quite handy to help me get output that's easy to parse without too much code. That's why I pick diff.Shawn– Shawn2012-02-08 13:55:17 +00:00Commented Feb 8, 2012 at 13:55
Add a comment
|
1 Answer
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
2 Comments
Shawn
Works! Could you elaborate how <(echo "string 1") can simulate a file in shell?
iblue
AFAIK It simply attaches two pipes to the diff command, like two STDIN's. But I don't know why and how it exactly works. That's why I asked the following question: stackoverflow.com/questions/9195117/two-pipes-to-one-command