Detect if homebrew package is installed - bash

I'm about to write a shell script to detect if several homebrew packages are installed in the system. Is there a way to use a brew command to achieve that?
I tried using the exit code of brew install <formula> --dry-run. But this builds the package if it is missing.

You can use
brew ls --versions myformula
to output the installed versions of the respective formula. If the formula is not installed, the output will be empty.
When using a recent versions of homebrew, which you can get with brew update, you can just run this (thanks Slaven):
if brew ls --versions myformula > /dev/null; then
# The package is installed
else
# The package is not installed
fi
That said, it is probably a good idea to check for the existence of the tool at all and not just checking for the respective homebrew package (e.g. by searching for the executable in the $PATH). People tend to install tools in a rather large amount of ways in practice, with homebrew being just one of them.

# install if we haven't installed any version
brew ls --versions $lib || brew install $lib
# install if we haven't installed latest version
brew outdated $lib || brew install $lib

What about?
for pkg in macvim ngrep other needed packages; do
if brew list -1 | grep -q "^${pkg}\$"; then
echo "Package '$pkg' is installed"
else
echo "Package '$pkg' is not installed"
fi
done

Easiest two-liners:
Step one, make sure it's installed
$ realpath . || brew install coreutils
This will print out the realpath of current dir, if not, then it will install it.
And it will not fail even realpath not found.
Step two, call it in your actual code:
$ realpath ${someDir}

For script and automation usage, I found out that brew bundle --help is very convenient.
If you do not want to use real bundle file, this snippet works fine in scripts:
brew bundle -v --file=- <<-EOF
brew "mc"
brew "ffmpeg"
brew "wget"
cask "cpuinfo"
cask "intel-power-gadget"
cask "unetbootin"
cask "vlc"
EOF
The good side of it, it will automatically detect if package is not installed, if it is outdated and only then will install it.
If you do not want updates, add a flag --no-upgrade. I have put -v for verbosity, as want more details, but you can skip it, or even use -q for even more silent run.

Related

msgmerge on macos Catalina

I am trying to run a unix command line for some i18n work on a project. Using gettext library my issue is about running msgmerge
$ msgmerge
zsh: command not found: msgmerge
I tried brew instal gettext and brew link gettext but with no success. This was working well on my previous machine, but can't make it work on a fresh install.
I can confirm binary is available in /usr/local/opt/gettext/bin/msgmerge.
Fixed. When running brew reinstall gettext, command actually give the answer:
If you need to have gettext first in your PATH run:
echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.zshrc
Export is only triggered when opening a new zsh prompt, PATH was not updated yet and worked when I re-openned a new window.
I tried running this brew link gettext --force, but I was getting:
$ brew install gettext
Warning: gettext 0.20.2 is already installed and up-to-date
To reinstall 0.20.2, run `brew reinstall gettext`
$ brew link gettext --force
Warning: Refusing to link macOS provided/shadowed software: gettext
If you need to have gettext first in your PATH run:
echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile
For compilers to find gettext you may need to set:
export LDFLAGS="-L/usr/local/opt/gettext/lib"
export CPPFLAGS="-I/usr/local/opt/gettext/include"
Then, I just added it to the system path with:
echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile
Related:
https://apple.stackexchange.com/questions/299048/can-not-use-the-gettext-which-is-installed-by-brew
Homebrew refusing to link OpenSSL
How to install gettext on MacOS X
Installed, just not linked

Homebrew: Install through a config file similar to pip's requirements.txt?

As title, with Python's pip, you could do:
pip install -r requirements.txt
to batch install packages with specified versions.
Does Homebrew have the same system?
I quickly browsed brew's man page and couldn't find obvious switches.
Homebrew Bundle is the preferred way.
brew bundle dump
brew bundle
Yes, there is.
For example you can update brew.txt whatever you want.
brew list > brew.txt
<brew.txt xargs brew install
For specific versions
node 5.5.0
node4-lts 4.3.1

Automated installation with Brew/cask in apps folder

I want create an automated script to use on a post installation.
For this reason i want use brew and cask to make it all.
So, i think that my script should start with that to install brew:
echo << "Installing homebrew..."
if test ! $(which brew); then
echo "Homebrew not found, Installing..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
Then i need to install some useful stuff as i see here:
http://lapwinglabs.com/blog/hacker-guide-to-setting-up-your-mac
So i put this too on my script:
# Install GNU core utilities (those that come with OS X are outdated)
brew install coreutils
# Install GNU `find`, `locate`, `updatedb`, and `xargs`, g-prefixed
brew install findutils
# Install Bash 4
brew install bash
# Install more recent versions of some OS X tools
brew tap homebrew/dupes
brew install homebrew/dupes/grep
$PATH=$(brew --prefix coreutils)/libexec/gnubin:$PATH
After that, the guide on the link says to install all the apps with cask and clean.
Here is my question.
I wish install and can update them in future using the classical Application folder of mac
How i can do that?
Maybe i should put this line:
export HOMEBREW_CASK_OPTS="--appdir=/Applications --caskroom=/usr/local/Caskroom"
Before all the commands to install the apps? May it works? (I have found this line here around)
If this line is correct can I update my apps using a brew/cask command?
Sorry for the dumbs questions, I've just discovered brew and cask yesterday :)
Any suggestion or example for this script is well accepted :)
brew cask install <formula> is supposed to symlink your app in Applications automatically.

Different version of pip installed when using sudo

Somehow, I've managed to install two versions of pip -- an older one (I assume installed using apt-get) when using:
pip --version
and a brand new version (I'm not sure how it got there) when using:
sudo pip --version
I have no idea how to get the new one uninstalled. Apt-get autoremove will get rid of the old one. Can somebody shed some light on what I've done wrong, and how to fix it?
When confronted with this mysteries you should always search the absolute path of what you're executing. You obviously are executing two different commands, and in order for that to happen the PATH environment variable of your user is different than the one of your root user. You can compare them by executing:
$ echo $PATH
$ sudo su
# echo $PATH
You can search through those directories and find the different pip scripts. My bet is that you have a local pip installation that lives in one of the directories mentioned in root's $PATH.
If you want to know the version of the pip installed by your package management tool, just issue the following command:
$ dpkg -l | grep python-pip
Run the following command if you want to know what files where installed by this package.
$ dpkg -L python-pip
Having found the culprit using this methods, you can now uninstall the version you don't need.

How to install JSON.pm perl module on OSX

I am trying to use the po2json parser/converter from the JS gettext library (http://jsgettext.berlios.de/), but when I try to convert I get this error:
Can't locate JSON.pm in #INC (#INC contains: /Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12 .) at ./po2json line 23.
BEGIN failed--compilation aborted at ./po2json line 23.
As far as I understand I am missing a perl module, namely JSON.pm, an I think it's this one: http://cpansearch.perl.org/src/MAKAMAKA/JSON-2.53/lib/JSON.pm
Now, how do I install it on OSX? I am using Mountain Lion.
I tried to do cpan install JSON, but it doesn't work, I get the following error:
Writing Makefile for JSON
MAKAMAKA/JSON-2.53.tar.gz
make -- NOT OK
'YAML' not installed, will not store persistent state
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible
Use homebrew to install cpanm:
brew install cpanm
Then you can install JSON with
sudo cpanm install JSON
The output said that make didn't return success. Probably due to some dependency.
Try using cpanm http://search.cpan.org/~miyagawa/App-cpanminus-1.5017/bin/cpanm, it's really hassle free and bootstrapped quite well (no need to install other stuff).
Install using this:
curl -L http://cpanmin.us | perl - --sudo App::cpanminus
cpan install JSON Having the JSON in all caps made it work for me.
I know this is an old topic, but wanted to share my solution after finding that I needed JSON when trying to use zap2xml (perl based). I was getting the error: "Can't locate JSON.pm in #INC..." after installing perl via Homebrew.
This is how I installed JSON via Homebrew. I will also include the steps required to install Homebrew (for those who would like to know). ;)
Prerequisites:
Install Xcode from the App Store (Download):
https://itunes.apple.com/us/app/xcode/id497799835
Install Command Line Tools for Xcode (Run in Terminal):
xcode-select --install
Install Homebrew (Run in Terminal):
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Update Homebrew (Run in Terminal):
brew update
brew upgrade
brew doctor (correct issues found by brew doctor. Instructions will be given by The Doctor. :P)
Install cpanminus (Run in Terminal):
brew install cpanm
Check perl (Run in Terminal):
which perl
perl -V (with a capital -V)
Check/Install JSON module (Run in Terminal):
sudo cpanm -v JSON
DONE.
Following worked for me:
Run on terminal:
sudo perl -MCPAN -e shell
you should get the cpan shell prompt and run below commond at cpan shell:
cpan[1]> install JSON
Regards,
Anand Choubey
this way is OK, Follows the Command:sudo apt-get install libjson-perl

Resources