devgp
devgp

Reputation: 1321

Bash Scripting: Output of find command with quoted variable to an array

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!

Upvotes: 0

Views: 1049

Answers (2)

doubleDown
doubleDown

Reputation: 8408

The problem lies here

searchSubDirectories=( $(find "${android_path}" -mindepth 1 -maxdepth 1 \
"${findNameCmdSubDir[@]}" -type d ) )

You are missing a -print at the end.


Also, regarding this block

for dir in "${excludeDirFromSearch[@]}"; do
    if [ $((dirCount++)) -eq ${#excludeDirFromSearch[@]} ]; then
            findNameCmdSubDir+=(-name "${dir}*")
    else
            findNameCmdSubDir+=(-name "${dir}*" -prune -o)
    fi
done

get rid of this

if [ $((dirCount++)) -eq ${#excludeDirFromSearch[@]} ]; then
            findNameCmdSubDir+=(-name "${dir}*")

You don't need it. You need -prune -o for all your excluded directories, i.e. your completed command string should have this form

    find -name EXCLUDE1 -prune -o -name EXCLUDE2 -prune -o -type d -print 

Compare this to what you have in mind (which won't work as you intended)

    find -name EXCLUDE1 -prune -o -name EXCLUDE2 -prune -type d

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

Define "not working". What is the result you expect/want to achieve? What is the actual result?

If find prints paths with spaces and you want to put those into a new array, try process substitution instead of command substitution:

findopts="-mindepth 1 -maxdepth 1 -type d"
...
searchSubDirectories=()
while read dir; do
  searchSubDirectories+=("$dir")
done < <(find "${android_path}" $findopts "${findNameCmdSubDir[@]}")

Note the space between the two < characters!

Upvotes: 1

Related Questions