I have a bash script which calls the 'find' command with multiple directories to prune.
Here is the entire script:
Code:
#!/bin/bash
SRCDIR="$HOME/test"
PRED="-wholename '$SRCDIR/skip1' -o -wholename '$SRCDIR/skip2'"
echo find $SRCDIR '\(' $PRED '\)' -prune -or -print
find $SRCDIR '(' $PRED ')' -prune -or -print
I have under SRCDIR the directories skip1, skip2, look1, and look2, each with a few files in it.
If I run the script and paste the echoed line into the bash prompt, it will print out:
Code:
/home/paul/test
/home/paul/test/skip1
/home/paul/test/look2
/home/paul/test/look2/file2.txt
/home/paul/test/look2/file1.txt
/home/paul/test/skip2
/home/paul/test/look1
/home/paul/test/look1/file2.txt
/home/paul/test/look1/file1.txt
I understand that the parentheses \( and \) need to be escaped to protect them from the shell, and this is the result I want.
I want the next line in the script to do the exact same thing, but called from the script. But the result is :
Code:
/home/paul/test
/home/paul/test/skip1
/home/paul/test/skip1/file2.txt
/home/paul/test/skip1/file1.txt
/home/paul/test/look2
/home/paul/test/look2/file2.txt
/home/paul/test/look2/file1.txt
/home/paul/test/skip2
/home/paul/test/skip2/file2.txt
/home/paul/test/skip2/file1.txt
/home/paul/test/look1
/home/paul/test/look1/file2.txt
/home/paul/test/look1/file1.txt
I can not figure out how to properly escape the parentheses characters within the script. I've tried all sorts of variations instead of '(' and ')', and here are the results:
( ... ) -> bash error: syntax error near unexpected token `('
'\(' ... '\)' -> find error: paths must precede expression
"\(" ... "\)" -> find error: paths must precede expression
\( ... \) -> same as '(' ... ')' , find prints out all directories
How can I properly tell find to treat the parentheses as precedence operators?