0

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.

5
  • What is "the external diff tool"? Commented Feb 8, 2012 at 13:48
  • what prograsm you are using for diff ? (more details please) Commented Feb 8, 2012 at 13:48
  • The linux diff tool. Simply $> diff Commented Feb 8, 2012 at 13:50
  • And what's wrong with PHP string functions to get the diff? Commented 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. Commented Feb 8, 2012 at 13:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Works! Could you elaborate how <(echo "string 1") can simulate a file in shell?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.