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!
unset -v 'list_apps[$index]'. The single quotes prevent the varable index from being expanded. Also, the single quotes on the RHS of the regex operator prevent the string from being interpreted as regex.