I’m trying to parse the android source directory and i need to extract all the directory names excluding certain patterns. If you notice below., for now i included only 1 directory to the exclude list, but i will be adding more.,
The find command doesn’t exclude the directory with name ‘docs’.
The commented out line works., but the other one doesn’t. For easy debugging, i included the min and maxdepth which i would remove later.
Any comments or hints on why it doesn’t work?
#! /bin/bash
ANDROID_PATH=$1
root=/
EXCLUDES=( doc )
cd ${root}
for dir in "${EXCLUDES[@]}"; do
exclude_name_cmd_string=${exclude_name_cmd_string}$(echo \
"-not -name \"${dir}*\" -prune")
done
echo -e ${exclude_name_cmd_string}
custom_find_cmd=$(find ${ANDROID_PATH} -mindepth 1 -maxdepth 1 \
${exclude_name_cmd_string} -type d)
#custom_find_cmd=$(find ${ANDROID_PATH} -mindepth 1 -maxdepth 1 \
# -not -name "doc*" -prune -type d)
echo ${custom_find_cmd}
Building up a command string with possibly-quoted arguments is a bad idea. You get into nested quoting levels and
evaland a bunch of other dangerous/confusing syntactic stuff.Use an array to build the
find; you’ve already got the EXCLUDES in one.Also, the repeated
-notand-pruneseems weird to me. I would write your command as something like this:The upshot is, you want the argument to
-nameto be passed tofindas a literal wildcard thatfindwill expand, not a list of files returned by the shell’s expansion, nor a string containing literal quotation marks. This is very hard to do if you try to build the command as a string, but trivial if you use an array.Friends don’t let friends build shell commands as strings.