0

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!

1
  • You wrote 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. Commented Mar 16, 2022 at 7:41

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Why index and not $index? And then, consequently, double quotes (or no quotes) in the unset statement.
@user1934428, (non-associative) array indexes are a "math context" in Bash. In a math context variable names can be used without preceding $ symbols. See ArithmeticExpression - Greg's Wiki. I omit the $ symbols in math contexts to de-clutter the code.
@user1934428, w.r.t. quoting, I always use single quotes except when expansions ($...) are required in quoted strings. That minimizes the risk of accidental expansions (e.g. because a string happens to have a $ in it). See Bash Pitfalls #57 (unset unquoted array element) for an explanation of why the quotes are necessary in unset 'list_apps[index]'.
Thanks. I was not aware about the numeric context in array indexing. I think this will simplify some of my scripts.....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.