I'm trying to store the output of the find command to an array.
I tried different things, and nothing seemed to work, couple of them in commented lines below. I guess the problem is with "${findNameCmdSubDir[@]}" is getting tokenized, and the the directories intended to exclude from search are ignored, and all directories are being listed.
If i give the find command without passing it to an array i.e., the plain find command, i see the expected output of listing all the other directories without the excludes.
#! /bin/bash
android_path=$1
excludeDirFromSearch=( doc build test unit-test script hardware prebuilt device . )
let "dirCount = 0"
findNameCmdSubDir=()
for dir in "${excludeDirFromSearch[@]}"; do
if [ $((dirCount++)) -eq ${#excludeDirFromSearch[@]} ]; then
findNameCmdSubDir+=(-name "${dir}*")
else
findNameCmdSubDir+=(-name "${dir}*" -prune -o)
fi
done
searchSubDirectories=()
searchSubDirectories=( $(find "${android_path}" -mindepth 1 -maxdepth 1 \
"${findNameCmdSubDir[@]}" -type d ) ) <<-- Not Working!
#find "${android_path}" -mindepth 1 -maxdepth 1 "${findNameCmdSubDir[@]}" \
# -type d | while read line; do
# echo "$line"
#done <-- Not working
find "${android_path}" -mindepth 1 -maxdepth 1 "${findNameCmdSubDir[@]}" \
-type d -print <-- works as expected !
echo "${searchSubDirectories[@]}"
Update
I tried escaping the double quotes and still no luck
searchSubDirectories=( $(find "${android_path}" -mindepth 1 -maxdepth 1 \"${findNameCmdSubDir[@]}\" -type d ) ) <<-- Not Working!