6

I am trying to install some softwares using brew cask and I would like to check if the package/formula is already installed through brew or other means

I have a script that installs a list of softwares using brew cask and I dont want it to install a software that is already present on the machine

To achive this I referred to this question: With Homebrew, how to check if a software/package is installed?

This is pointed me in the right direction but I can only avoid installing software that is installed via brew cask.

If the software is installed by downloading the dmg and is already present in the /Applications/ folder then it does not recognize that and it continues to install the software.

Is there a way to figure out if the application is installed via brew or any other means before my script starts installing it

nohillside
  • 100,768
Nick Div
  • 163
  • "If the software is installed by downloading the dmg and is already present in the /Applications/ folder then it does not recognize that and it continues to install the software."

    Use find command to search the /Applications and ~/Applications folder for the target formula. List out all the matches in the console and ask user if they want to continue install or not.

    – anki Sep 10 '20 at 05:00
  • @anki But the formula name and the Application name are not the same. For e.g. google chrome formula is google-chrome while the application name is 'Google Chrome' In this scenario I will have to keep on editing the script if the app decides to change its name. – Nick Div Sep 10 '20 at 05:27
  • regex, case insensitive search etc can be helpful here. I don't know any central list of all installed apps. You just have to search.. – anki Sep 10 '20 at 05:29
  • @anki I guess. I was hoping there was some way I could have extracted the app name from formula by brew before installing but it seems i do not have any other option. – Nick Div Sep 10 '20 at 05:32
  • 1
    You could parse the output of brew cask info --json=v1 google-chrome or brew cask info google-chrome to find the name the cask will install to. It won't help in case of renamed applications though. – nohillside Sep 10 '20 at 06:11
  • @nohillside If apps are renamed then I dont really care. The user will have to figure it out but this command would definitely make my life easier. Thanks – Nick Div Sep 10 '20 at 17:35
  • @nohillside That did the trick. Awesome, thanks – Nick Div Sep 11 '20 at 04:02

2 Answers2

4

I assume you know the name of the application. Then just test for existence of the application

if [[ ! -d "/Applications/APP-TO-CHECK.app" ]]; then
    # install APP-TO-CHECK
fi 
nohillside
  • 100,768
  • But the formula name and the Application name are not the same. For e.g. google chrome formula is google-chrome while the application name is 'Google Chrome' In this scenario I will have to keep on editing the script if the app decides to change its name. – Nick Div Sep 10 '20 at 05:27
  • I tried to run your script but it gives me the following error: syntax error in conditional expression -> if [[ ! -d /Applications/Google Chrome.app ]]; then – Nick Div Sep 10 '20 at 05:33
  • Adding quotes to it fixed the issue – Nick Div Sep 10 '20 at 05:35
  • 1
    @NickDiv Names with spaces always need to be quoted in the shell. As for the renaming issue: I don‘t see a failsafe way to cover all possible scenarios, and honestly consider this to be rare. If necessary I would just test for all known names. – nohillside Sep 10 '20 at 05:50
  • That's the POSIX standard for test. It's sufficient to write [ and ]. This works in any shell. There is no need for [[ and ]]. See BashFAQ/031: http://mywiki.wooledge.org/BashFAQ/031 – Christopher Sep 14 '20 at 19:22
1

This is a primitive solution but it might help.

  1. Download this app (it allows you to search hidden files.)
  2. Search for the name of your application/repo you want to download.
  3. If it is not there, then it is safe to say you don't have it.