sardean
sardean

Reputation: 171

for looping over an array

I am trying to resolve an issue with a bash script that is intended to search through each users home directory in /Users/ and find two different directories, stored in array "SUBDIRS." If these directories exist, I want to remove with the recursive and force options. If they do not exist I want the script to continue looking for the next directory, next home folder, etc.

#!/bin/sh


err=0




SUBDIRS=(
    "Library/Application Support/Spotify"
    "Library/Caches/com.spotify.client"
)


for HOMEDIR in /Users/*; do

    for SUBDIR in ${SUBDIRS}; do

        DIR="${HOMEDIR}/${SUBDIR}"

            if [[ -d "${DIR}" ]]; then
                rm -rf "${DIR}"
                echo "${HOMEDIR}/${SUBDIR} has been removed."
                APP=$(find "${HOMEDIR}" -name [sS]potify.app)
                rm -rf "${APP}"
            fi

    done

done

exit $err

Upvotes: 1

Views: 147

Answers (1)

Dennis Williamson
Dennis Williamson

Reputation: 360065

You need to signify that it's an array to be expanded (and quote it).

for SUBDIR in "${SUBDIRS[@]}"; do

You should quote the pattern in the find command so find will expand it instead of the shell.

APP=$(find "${HOMEDIR}" -name '[sS]potify.app')

Upvotes: 3

Related Questions