Help with a bash script to proceed if bundle id is found

bashterminal

Looking for some help writing a bash script that checks if a particular bundle id is installed and proceeds to install the package if not found.

Found that we can use "mdfind kMDItemCFBundleIdentifier = "com.hnc.Discord" (for example) returns the location for the particular bundleid if the app is not found nothing comes back. Maybe I can use this with an if statement somehow?

Any help would be much appreciated.

Best Answer

mdfind does not produce an unsuccessful exit code but you could pipe the results to grep which does. The code would look something like this:

mdfind "kMDItemCFBundleIdentifier = com.apple.Terminal" | grep -q Terminal || installer ....

|| is logical or.

It could also be written

if mdfind "kMDItemCFBundleIdentifier = com.apple.Terminal" | grep -q Terminal
then
  :
else
  installer ...
fi