0

I have the need to pass multiple parameters to find dynamically. I'm doing that using this function:

GEN_FIND_PARAMS() {
    PARAM=""

    for WORD in $1; do
        ADD=""

        if [ X"$PARAM" != "X" ]; then
            ADD="-o"
        fi
        ADD="-name '$WORD' $ADD"

        PARAM="$ADD $PARAM"
    done

    echo $PARAM
}

If I run the function by itself, I get correct results:

SHD_FILES="*.rev *.db *.cdb"
GEN_FIND_PARAMS "$SHD_FILES"

Outputs correctly

-name '*.rev' -o -name '*.db' -o -name '*.cdb'

However, when I using it fully, it just doesn't find anything:

find /dev/shm $(GEN_FIND_PARAMS "$SHD_FILES")

Outputs nothing, even though I know the files are there, because running it plainly works:

find /dev/shm -name '*.rev' -o -name '*.db' -o -name '*.cdb'

Is there something wrong in the value substitution? I'm also open to other ways to do this (there must be a better one)

Délisson Junio
  • 288
  • 3
  • 10
  • [How do I store a command in a variable?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) Not by putting quotes in it. – Gilles 'SO- stop being evil' Aug 16 '17 at 22:52
  • @Gilles I'm puzzled. Putting the quotes as per the linked question explains, the command substitution seems to work correctly, however `find` seems to get it wrong: `find: unknown predicate \`-name '*.rev'` (continues with the other predicates) – Délisson Junio Aug 16 '17 at 22:58
  • The linked answer explains that you can't do it that way. You're passing the quotes to the `find` command. Read my answer! – Gilles 'SO- stop being evil' Aug 16 '17 at 23:00

0 Answers0