如何从数组中删除某些项目

How to remove certain items from an array

我正在尝试使用 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
}

事实是 Parallels Desktop 已安装,并且在填充 mdfind 数组时会得到很多这样的条目:

/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

我正在尝试过滤掉(未成功):

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

有什么建议吗?

谢谢!

尝试:

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

因为单引号,=~ '^(?!.*\(Parallels\)).*' 只匹配包含 文字字符串 '^(?!.*\(Parallels\)).*' 的字符串([=13= 没有特殊含义) ]、(. 等)。如果您删除引号,它仍然不起作用,因为它使用 Bash.

不支持的正则表达式功能

上面的代码使用 Bash glob 模式匹配来匹配包含文字字符串 (Parallels).

的字符串