javiers
javiers

Reputation: 23

How to remove certain items from an array

I am trying to get a list of all Apps plus their versions using mdfind:

function get_mac_apps_info {
    local list_apps=()
    local app_version
    local plist_info_app_path
    local plist_field="CFBundleName"

    readarray -d '' list_apps < <(mdfind -0 "kMDItemContentType == com.apple.application-bundle")

    for index in "${!list_apps[@]}"
         do [[ ${list_apps[$index]} =~ '^(?!.*\(Parallels\)).*' ]] && unset -v 'list_apps[$index]'
     done

        for app_path in "${list_apps[@]}"; do

        plist_info_app_path="$app_path/Contents/Info.plist"

        if [[ -f "$plist_info_app_path" ]]; then

            app_version="$(get_version_from_plist "$app_path" 2>/dev/null)"
            app_name="$(get_field_from_plist "$app_path" "$plist_field" 2>/dev/null)"
            if [[ $app_version ]]; then
                echo "$app_version;$app_name"
            fi
        fi

    done
}

Thing is Parallels Desktop is installed and gets a lot of entries like these when populating the mdfind array:

/Users/user-test/Applications (Parallels)/{8dcf6541-4642-4aa0-b6ef-f73b59c0005e} Applications.localized/Command Prompt.app
/Users/user-test/Applications (Parallels)/{9bfd84de-a9b0-445d-afd5-c95690c3d1ea} Applications.localized/Command Prompt.app

I am trying to filter this out (unsuccessfully):

    for index in "${!list_apps[@]}"
         do [[ ${list_apps[$index]} =~ '^(?!.*\(Parallels\)).*' ]] && unset -v 'list_apps[$index]'
     done

Any suggestions?

Thanks!

Upvotes: 0

Views: 59

Answers (1)

pjh
pjh

Reputation: 8124

Try:

for index in "${!list_apps[@]}"; do
    [[ ${list_apps[index]} == *'(Parallels)'* ]] && unset 'list_apps[index]'
done

Because of the single quotes, =~ '^(?!.*\(Parallels\)).*' only matches strings that contain the literal string '^(?!.*\(Parallels\)).*' (no special meaning is given to ^, (, ., etc.). If you remove the quotes it still doesn't work because it uses regular expression features that aren't supported by Bash.

The code above uses Bash glob pattern matching to match strings that contain the literal string (Parallels).

Upvotes: 1

Related Questions