Re: Bash and/or find question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bill Gradwohl wrote:

Because "&&" is a shell construct and you're giving it to test as an argument.



I realize that && is a shell construct. As I mentioned in the original post, if I key in : test -e ../two/./x.java && diff ./x.java ../two/./x.java at a command prompt, everything works as expected. The shell separates the test from the diff and only executes the diff if the test is true.

My question is why when find submits the same string to the shell that it comes out with an error.


Sorry, it's late at night and I'm not doing too well with the explanation. When you type


test -e ../two/./x.java && diff ./x.java ../two/./x.java

The shell splits this into tokens. Some tokens have special meaning, in this case "&&" separates two commands. This much you know. However, when you do

find ... -exec test -e ../"$2"/{} '&&' diff {} ../"$2"/{} ';'

find is contstructing a program argument list. It basically does this:

execlp("test", "test", "-e", "../two/x.java", "&&" "diff" "x.java" "../two/x.java", NULL);

What you have here is an invocation of the "test" program (the one in /usr/bin, not the one built into the shell, the shell isn't involved here at all). If you want shell semantics then you need to do

find ... exec sh -c "test -e ../$2/{} && diff {} ../$2/{}" ';'

In this case, find constructs a program with three arguments, like this:
execlp("sh", "sh", "-c" "test -e ../two.java && diff x.java ../two/x.java", NULL);


As I said, I don't do that -- diff or cmp are quite capable of failing when a file doesn't exist and also the "{}" syntax never seems to work properly for me ... but that was years ago when I gave up on it and build commands up a different way.

jch

jch



-- Shrike-list mailing list Shrike-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/shrike-list

[Index of Archives]     [Fedora Users]     [Centos Users]     [Kernel Development]     [Red Hat Install]     [Red Hat Watch]     [Red Hat Development]     [Red Hat Phoebe Beta]     [Yosemite Forum]     [Fedora Discussion]     [Gimp]     [Stuff]     [Yosemite News]

  Powered by Linux