Bash check if specific version of brew formula is installed - bash

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!

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 qt#5.5.) This just made it easier to install multiple (especially older) versions of a formula.

Related

Command to download latest version of nvm?

I am trying to install the latest version of nvm so (per this article) I ran:
nvm install 4.0
It worked. But I want to make sure I install the latest version of nvm as they might have released a newer version after the article was written.
Is there a command I can run to download the latest version of nvm?
These answers are all about updating the version of Node, when the original question is about updating the version of the NVM tool itself.
The script to update NVM is:
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
and you can find the list of the latest releases here:
https://github.com/nvm-sh/nvm/releases
More details found here:
https://techsparx.com/nodejs/howto/nvm-deprecate-mirror.html
You can run nvm install node to get the latest node release.
The only solution I have found so far involves a two-step process.
Step 1: List all the released versions.
nvm ls-remote
At the present time, the last item on this list is...
...
...
...
v6.1.0
Step 2: Download latest version on the list of available versions.
nvm install 6.1.0
To automatically pick the most recent nvm tool version instead of having to look up the part like v0.39.1 for the wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash command the following bash script can be used:
#!/bin/bash
nvm_string="nvm-sh/nvm";
git_latest_version() {
basename $(curl -fs -o/dev/null -w %{redirect_url} https://github.com/$1/releases/latest)
}
latest_version_number=$(git_latest_version "${nvm_string}");
#echo ${latest_version_number}
wget -qO- https://raw.githubusercontent.com/${nvm_string}/${latest_version_number}/install.sh | bash
Usage (Linux, Mac):
Make a new file like install-nvm.sh e.g. with vim or vi or whatever. Then copy paste the above script (type i to insert, type :wq to save it), then do sh install-nvm.sh;
This solution was based on
Github nvm repo

Download different version of grep in cygwin?

I'd like to download a different version of grep in Cygwin. Currently, I have version 2.21, but I'd like to get version 2.5.1 (this is what runs on Mac OS by default, and I'm more familiar with that).
I obviously don't want to run the entire setup again. Is there a way to get the Mac OS version (i.e. 2.5.1) without running setup all over again? Thanks. <3
Compiling is always a possible choice: grep lives here: ftp://ftp.gnu.org/gnu/grep/, and given the tarball (ftp://ftp.gnu.org/gnu/grep/grep-2.5.1.tar.gz),
tar xf 2.5.1.tar.gz
cd 2.5.1
./configure
make && make install
(this will probably install into /usr/local/bin — you should read the instructions, e.g., the --prefix option to suit your own needs).
That assumes you are developing, and have installed gcc (the Cygwin setup program helps in that case).

how to install libX11 on OSx 10.9?

I am trying to install ROOT (cern.root.ch). When I run ./configure , I get a message that libX11 is missing and must be installed.
I did some research and found that I need to install
) XQuartz (I already have the latest version.)
) Command line tools in Xcode.
I tried installing Command Line Tools from apple's developer website. The installation goes through smoothly but how do I know whether it has been installed? I still get libX11 missing error with root's configure command.
I also tried xcode-select --install and it once went through smoothly and then later again gives error saying this package is no longer maintained - or something of that sort.
I understand I may have multiple installations... But I am still facing the problem of not having libX11 and not being able to install ROOT.
Thanks,
Hershal.
This link and the one referenced in it suggests you use homebrew (brew) to install it
$ ruby <(curl -fsS https://raw.github.com/mxcl/homebrew/go)
$ brew doctor
Remember to add the Homebrew directory to your PATH by adding the directory (found with brew --prefix) to your .bashrc, .zshrc or whatever shell file you’re using (.bashrc is the OS X default). We’ll also add the XQuartz binaries to the PATH in case anything needs them in the future.
export PATH=/usr/local/bin:/opt/X11/bin:$PATH
Start a new Terminal session to pick up the changes.
Now that Homebrew is installed, we can use it to install the required dependencies. Each may take some time as Homebrew generally compiles from source.
$ brew install gfortran # Fortran compiler
$ brew install python # Python interpreter
$ brew install pcre # Regular Expressions library
$ brew install fftw # Fast Fourier Transforms
$ brew install cmake # Cross-platform make
install root
$ brew tap homebrew/science
$ brew install --with-cocoa root
You don't say whether you have installed XCode as well as the commandline tools but I think you will need it

Incorrect check of SIP version during PyQt installation

I have been trying to install PyQt on my mac (OSX 10.6.8), but I still having this problem:
When I try to install PyQt-mac-gpl-4.9.6, I get this
Error: This version of PyQt requires SIP v4.14.2 or later.
The weird thing is that when I want to check for sip version on the console and write
sip -V
,I get the correct version: 4.14.2
I have checked my PATH but non of the online tips that I have found have helped me. How do I fix this?
I would recommend checking to see if you have sip installed in different locations, with the newer version being in a path that is not at the head of your PATH.
mdfind -name sip | grep '/bin/'
This should tell you all of the sip binaries on your computer. Check the version of all of them, and then make sure that path is at the front of PATH
What it could actually be, regardless of it seeming like you only have one sip, is a leftover .pyc file that is getting picked up before your intended sip. Try this to confirm:
$ python
>>> import sipconfig
>>> print sipconfig
This will tell you where it is finding sipconfig. If it looks like the wrong spot, or if you check it and all that is left is the pyc file, remove it. Then your newer sip should be found properly.
I had a similar issue after installing sip 4.14.2 over a previous version of 4.13.1 on Mac OSX 10.7.4 and Python 2.7. Strangely, the version of sip from the command line was different from the version from the Python sipconfig module.
$ sip -V
4.14.2
$ python -c "import sipconfig; print sipconfig.version_to_string(sipconfig.Configuration().sip_version)"
4.13.1
I ran through the install again, this time with running the install step as 'sudo make install' and the two versions matched at 4.14.2 and the PyQt instal was successful.
$ cd ~/Downloads/sip-4.14.2/
$ python configure.py
$ make
$ sudo make install

Updating grep for Mac OS 10.7

I would like to update grep on my Mac to a more recent version than 2.5.1, which came with Mac OS 10.7.2. My question is: what is the best way to update grep (or any similar program) for the Mac? I could use Fink or MacPorts to install a new version and set my path variable to look in the appropriate branch of the file tree, or I could update the grep program in usr/bin, or perhaps there's another approach that I haven't considered. Because I am relatively new to the command line and the Unix back-end of the Mac, I'm concerned about breaking something. That said, I'd certainly be willing to compile the most recent stable release of grep from source and install it in /usr/bin if that's the appropriate method. In case anyone wonders why I'd want to update grep from 2.5.1, I have two reasons: 1st, I'm learning to use grep with a reference book that is based on 2.5.3 (probably similar, I know); 2nd and more importantly, I want to learn how to update such programs simply as a matter of administrating my own system effectively.
As you said, you may use Fink, MacPorts, etc...
But if you just want to update grep, you may want to grab the sources, and compile them.
If you decide to go with this option, don't install it in /usr/bin.
If you do so, you will overwrite something needed by your OS.
So with another version, you may encounter problems, as the OS will except another version.
And also, if you do so, you'll have problems when updating your OS, as it might overwrite your own version.
So if you want to compile it, place it in /usr/local/bin (usually with the --prefix option), and update your path environment variable.
This is the safe way.
Usually, compiling such a program is just the standard ./configure, make and sudo make install stuff.
But be sure to take a look at the compiling options first, by typing:
./configure --help
The following is a very elegant solution from http://www.heystephenwood.com/2013/09/install-gnu-grep-on-mac-osx.html
# Enable dupe and install
brew tap homebrew/dupes
brew install homebrew/dupes/grep
# Install the perl compatible regular expression library
brew install pcre
# Add the symlink to a place in $PATH
ln -s /usr/local/Cellar/grep/2.14/bin/ggrep /usr/bin/ggrep
# Add an alias
alias grep="ggrep"
# Verify you got it!
$ grep --version
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
# If you want it to be permanent, you can add the alias line to your ~/.bash_profile
# You probably want the alias to stick after reboots
echo 'alias grep="ggrep"' >> ~/.bash_profile
It's gotten easier recently:
brew install grep
Which causes lines like the following:
==> Installing dependencies for grep: pcre
==> Installing grep dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pcre-8.43.high_sierra.bottle.tar.gz
🍺 /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing grep
==> Downloading https://homebrew.bintray.com/bottles/grep-3.3.high_sierra.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring grep-3.3.high_sierra.bottle.2.tar.gz
==> Caveats
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
==> Summary
🍺 /usr/local/Cellar/grep/3.3: 21 files, 880.7KB
==> Caveats
==> grep
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
Then you would use:
ggrep --color=auto
Anywhere where you would have executed grep before.

Resources