3

I am writing a script to automate the setup of development environments on a mac, however I am running into an issue on some peoples macs where cask (Homebrew extension) does not install. Is there a way I can check if cask has been installed in bash?

Ideally I am looking for something like:

if caskIsInstalled then
    # do stuff...
fi

I have been able to verify that brew itself is installed using the type command, but I can't figure out a way to verify cask.

if ! type "brew" > /dev/null; then
    echo "Failed to install Homebrew"
    # do stuff...
fi
Rylander
  • 19,449
  • 25
  • 93
  • 144
  • Try to install it? Look at the output from `brew list`? See if `brew info cask` says anything useful in this case? – Etan Reisner Oct 12 '15 at 20:27
  • @EtanReisner `brew info cask` returns `Error: No available formula for cask`. Is there something I can use to parse the out put in bash? – Rylander Oct 13 '15 at 14:39
  • 3
    A down vote, without any feed back as to why, does not help anyone improve the quality of a question. – Rylander Oct 13 '15 at 14:40
  • What does it return when it is installed? What exit status does it return with in both cases? Ideally, it will return `0` when it is installed and non-zero when it isn't and you can just use that. – Etan Reisner Oct 13 '15 at 15:07
  • @EtanReisner That is the way it behaves. Thanks, I did not think to check the return code. I will be able to come up with something out of this. – Rylander Oct 13 '15 at 15:19
  • `if brew info brew-cask; then : do something with cask else : throw an error or install it; fi` Alternatively, if you are just going to install it when it isn't there then you can probably skip the checking and just try to install it from the get-go. – Etan Reisner Oct 13 '15 at 15:24
  • @EtanReisner Can you put that in an answer? – Rylander Oct 13 '15 at 15:27
  • @EtanReisner The issue that is that cask fails to install, not that it is not installed. The fix involves fixing directory permissions. – Rylander Oct 13 '15 at 15:28
  • And then presumably reinstalling cask, right? You could just check for those permissions issues and fix them (or ask about fixing them and the installing cask). That would have avoided this need (though it is clear that it can be handled this way as well). – Etan Reisner Oct 13 '15 at 15:35

2 Answers2

3

Given that brew helpfully returns a sane exit status when a package is installed versus when it isn't, it is possible to avoid needing the pipeline and grep entirely.

if ! brew info brew-cask &>/dev/null; then
    : Do something because cask is not installed
else
    : Do something when cask is installed
fi

If brew has a -q/--quiet option which silences the error you get from using info on an uninstalled package then that can be used instead of the redirection.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • 1
    Nice, though if the test is to be fully silent, `&>/dev/null` should be used to also silence _stdout_ output in case the test succeeds. – mklement0 Oct 13 '15 at 15:55
2

This seems to work:

if brew info brew-cask | grep "brew-cask" >/dev/null 2>&1 ; then 
   echo cask is installed
fi
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • `grep -q` is likely better than redirecting output but using the return status of `brew` itself is even better then that. – Etan Reisner Oct 13 '15 at 15:24
  • @EtanReisner I actually tried `brew ... | grep -q ...`, but if it isn't installed, you get a broken pipe error. So, as you say, I guess you can dispose of the `grep` and use the exit status, but you'll still need the `stderr` redirection. – Mark Setchell Oct 13 '15 at 15:31
  • Yes, without `grep` you need the redirections. You shouldn't get a broken pipe though unless something has ignored SIGPIPE in the environment (or `brew` doesn't normally handle SIGPIPE correctly I think). See http://stackoverflow.com/q/33020759/258523 (and the link in my comment) for some discussion about that topic). – Etan Reisner Oct 13 '15 at 15:34
  • 2
    @EtanReisner Please go ahead and put your suggested answer - it is better than mine. – Mark Setchell Oct 13 '15 at 15:36