find command from shell scipt (ksh) problem

Hi experts,

I have a simple shell script as follows.

#!/bin/ksh
FIND_STRING="\( -name 'testfile*.Z' -o -name 'DUMMY_*' \) "
find /tmp -type f $FIND_STRING -print

When I run this with ksh -x testscript, I get the following output.

+ FIND_STRING=\( -name 'testfile*.Z' -o -name 'DUMMY_' \)
+ find /tmp -type f \( -name 'testfile
.Z' -o -name 'DUMMY_*' \) -print
find: 0652-009 There is a missing conjunction

If I cut and paste the second line of the output into the dollar prompt, it works fine. Donot know what is happening . I had posted a similar error few weeks before, but now I could pinpoint to a 2 liner.

Thanks in advance for your help.

The variable FIND_STRING is not being expanded by the shell when the find is run and that's why when you replace it with its contents the find command runs alright.

Well, "$FIND_STRING" is being evaluated in that the variable name is being replaced with the variable contents. However, the contents themselves are not being evaluated...that would be double-evaluation, a 15-yard penalty.

You could either use the command 'eval' in front of the 'find' command, or just remove the backslashes when you set the string.

Here is the ouput when you do set -x on the script

+ FIND_STRING=$'\\( -name \'testfile*.Z\' -o -name \'DUMMY_*\' \\)'
+ echo '\(' -name $'\'testfile*.Z\'' -o -name $'\'DUMMY_*\'' '\)'
\( -name 'testfile*.Z' -o -name 'DUMMY_*' \)
+ find /tmp -type f '\(' -name $'\'testfile*.Z\'' -o -name $'\'DUMMY_*\'' '\)' -print
find: \(: unknown option

If you change the find command line to

eval find /tmp -type f $FIND_STRING -print

things work as expected.

Thanks a lot for your explanation and the eval works fine. I had not used eval before and didn't know it is so vital. I haven't fully digested the solution but will spend some time on it.

Thanks again for your kind help.

pfmurphy, just wondered how did you get the output you had posted with set -x? In my system (ksh on aix) the output is same as I posted in the beginning.

Basically eval causes a shell to parse a command line twice. The following simple example should help clarify things

name=PATH
echo $name
eval echo $name
eval echo $$name
eval echo \$$name