Click to See Complete Forum and Search --> : find -exec strangeness


rmabry
08-23-2002, 11:45 PM
Sorry if this is a newb-brainer. Can someone explain why the outputs of the following two commands differ?

find . -exec lc {} \;

find . -exec echo `lc {}` \;

The command lc is the following simple shell script:

#! /bin/sh
echo $1 | tr "[A-Z]" "[a-z]"

Obviously, all the script does is translate upper to lower case. In a directory containing files having names of mixed case the outputs of the two commands differ. With the first command I get what I'd expect --- the filenames are listed but converted to lowercase. But the second command leaves the names unchanged.

Thanks for the help,

Rick

furrycat
08-24-2002, 12:58 AM
There's nothing strange about it at all. By enclosing {} in the backticks you are passing it as a literal argument to lc and it is no longer passed to find.

dfx
08-25-2002, 01:32 PM
Furrycat is right, but his reply confused me a bit at first. :) Let me try to clarify:

What happens is that the backticks are expanded by the shell before find actually runs. The command "lc {}" is run and its output ("{}") is inserted where the backticks were, and so the effective command line becomes:

find . -exec echo {} \;

What you can do is escape the backticks with backslashes to pass them to find, but that will only work if find execs the command line in a shell (and I don't know if it does that; probably not).

furrycat
08-25-2002, 11:03 PM
strace -f -e trace=execve find . -exec echo {} \; shows you that it does not :(