Reputation: 169
I am making a script in bash to configure some MacOS machines that has some brew formulas installed. I first want to check if a specific version of that formula has been installed. If not installed, the script will install it.
The thing is that I cannot figure out how to check this. E.g. brew ls --versions openssl
returns all installed versions. But I want to check for a specific version, kinda similar to gems gem list -i cocoapods -v 1.3.1
. The reason for this is that some projects require to have specific versions installed (like openssl).
Is there a possibility to do this? Note that there are some brew formulas to be checked, so I want to keep it as lightweight as possible :)
I have something in mind like this (but with the correct check of course):
if ! NOT_INSTALLED; then #check to see if ruby -v 2.2.2 has been installed
brew install ruby -v 2.2.2
else
echo "Skipping install ruby..."
fi
Thanks in advance!
Upvotes: 5
Views: 2602
Reputation: 6430
There's not a builtin Homebrew command to check if a version is installed, but it's pretty straightforward to do with some shell scripting. For example,
brew list --versions | grep qt | cut -d " " -f 2
lists all the installed versions of Qt I have on my machine, and prints:
5.10.0_1
5.7.1
You can further refine this to see if a specific version is installed:
brew list --versions | grep "qt.* 5.7"
will print any version of Qt 5.7.
A simple shell function which will search for a specific version of a specific formula might look like:
formula_installed() {
[ "$(brew list --versions | grep "$1.* $2")" ]
return $?
}
Usage:
$ formula_installed qt 5.7
$ echo $?
1
$ formula_installed qt 8
$ echo $?
0
A note about the .*
regex in the grep
call. Originally, multiple versions of a formula were maintained in the homebrew/versions
tap. This was deprecated, in favor of keeping older versions in homebrew/core
but appending @<version>
to their name. (For example, you can install Qt version 5.5 by doing brew install [email protected]
.) This just made it easier to install multiple (especially older) versions of a formula.
Upvotes: 4