Escaping the parentheses characters in a bash script which calls find with -prune
I have a bash script which calls the 'find' command with multiple directories to prune.
Here is the entire script: Code:
#!/bin/bash If I run the script and paste the echoed line into the bash prompt, it will print out: Code:
/home/paul/test 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 ( ... ) -> 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? |
I found the following workaround:
bash -c "find $SRCDIR \( $PRED \) -prune -or -print" But this is just a horrible ugly hack, calling bash from within the script. |
try to cast eval for that:
Code:
eval "find $SRCDIR \( $PRED \) -prune -or -print" Code:
#!/bin/bash |
Is it just me or does the first post have the brackets NOT escaped??
Quote:
|
I can't test this 'cos I don't have the files to search, but I had a problem using find with ssh (I was tying to use find within a script to search for files on another computer). My solution was to escape the backslash with two more backslashes like this
Code:
file_list=($(ssh eccles find ~/save/sources \\\( -path ~/save/sources/.git -o -path \ |
This works:
Code:
#!/bin/bash |
Thanks, grail.
I see the difference is the removed single-quoting of $SRCDIR/skip1. If it is quoted it will look in the skip directories. For my purposes not having the quotes will work, but I'm afraid if will fail on a directory with a space in its name. I guess that's a separate question though...I will work on it. |
You could try putting in escaped quotes (\") around the path to be excluded to help preserve the spaces.
|
Quote:
|
Where there is a will there is a way :)
Code:
#!/bin/bash Code:
eval find $SRCDIR \\\( $PRED \\\) -prune -or -print Code:
PRED="\( -wholename \"$SRCDIR/skip1\" -o -wholename \"$SRCDIR/skip2\" \)" |
All times are GMT -5. The time now is 01:34 PM. |