Unattended (no-prompt) Homebrew installation using expect - macos

According to the Homebrew installation instructions, the following command can be used to install:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
This works, but it needs user input two times; to confirm the install and in a sudo prompt invoked by the script:
$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /usr/local
Password:
Homebrew doesn't have arguments for unattended installations, so the only option I can think of is to programatically input the expected data. I tried using expect, but I can't quite get the syntax right:
$ expect -c 'spawn ruby -e \"\$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)\";expect "RETURN";send "\n"'
ruby: invalid option -f (-h will show valid options) (RuntimeError)
send: spawn id exp7 not open
while executing
"send "\n""
What am I doing wrong?

Unattended installation is now officially supported
https://docs.brew.sh/Installation#unattended-installation

If you want to create a setup script which installs homebrew silently then just pipe a blank echo to the homebrew's installer. Then redirect the results to /dev/null as #charles-duffy suggested.
#!/usr/bin/env bash
# install.sh
URL_BREW='https://raw.githubusercontent.com/Homebrew/install/master/install'
echo -n '- Installing brew ... '
echo | /usr/bin/ruby -e "$(curl -fsSL $URL_BREW)" > /dev/null
if [ $? -eq 0 ]; then echo 'OK'; else echo 'NG'; fi
$ ./install.sh

Related

Installing brew from macOS app's pre/postinstall script

In my Packages.app's preinstall script I was able to install brew from a modified version of brew's install.sh that removed the sudo check:
#!/bin/bash
##preinstall
if brew ls --versions wget > /dev/null; then
# The package is installed
osascript -e 'tell app "Finder" to display dialog "The package is installed"'
else
# The package is not installed
osascript -e 'tell app "Finder" to display dialog "The package is not installed"'
/usr/bin/su root -c ./brew-install.sh
fi
exit 0
This is the line I removed from brew's install.sh to get this to work:
if [[ "${EUID:-${UID}}" == "0" ]]; then
I don't know what "${EUID:-${UID}}" is exactly.
This is what I've tried so far unsuccessfully instead of using su root:
# /bin/bash -c ./brew-install.sh
# sudo dseditgroup -o edit -a $USER -t user admin
# sudo /usr/bin/su $USER -c ./brew-install.sh
# /usr/bin/su ladmin -c ./brew-install.sh
# nohup /usr/bin/su $USER -c /bin/bash -c ./brew-install.sh &
Notably trying to add $USER to admin group from here: https://apple.stackexchange.com/a/76096/261453
Any ideas/solutions?
You can install Brew without root by using the "untar install" option for brew:
Why brew installation needs sudo access?
See Brew docs here:
https://docs.brew.sh/Installation#untar-anywhere
If Brew is still blocking for root reasons you can modify Library/Homebrew/bin/brew.sh to remove the root check and then from postinstall, rm the existing brew.sh and replace if with your copy that you added in Packages.app's Script's Addition Resources:

Need sudo access on macOS - Installing Hashcat through Brew

I am trying to install Hashcat using the instructions on Brew: https://brewinstall.org/install-hashcat-on-mac-with-brew/
When I enter the first command:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
on terminal, it says:
Need sudo access on macOS (e.g. the user myname to be an Administrator)!
However, I am already an administrator when I check in Users&Groups in Systems Preferences. Typing sudo whoami also gives me back root
Can someone please help me out with this?
I'm pretty sure all you have to do is add sudo in front of the command like this:
sudo ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

Bypassing prompt (to press return) in homebrew install script

Very simple script that installs homebrew:
#!/bin/bash
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
The output gives:
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press RETURN to continue or any other key to abort
How do I press enter in a script like this? Would expect be the best route?
Reading the source of https://raw.github.com/Homebrew/homebrew/go/install -- it only prompts if stdin is a TTY. If you redirect stdin from /dev/null, it won't prompt at all. So:
ruby \
-e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
</dev/null
This is what yes is for:
yes '' | ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Per the lead maintainer of Homebrew:
echo | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
this will work
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" < /dev/null
This works fine for me,
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null

Find the install location of brew on OS X

I'm creating a BASH scrip which requires a couple of applications to be installed. ffmpeg and sox
To ensure they are in place when my script runs I first check for the installation of Homebrew with :
#!/bin/bash
which -s brew
if [[ $? != 0 ]] ; then
# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
fi
Then I check that sox and ffmpeg are installed with :
echo "---- checking for sox ----"
which -s sox || /usr/local/bin/brew install sox
echo "---- checking for ffmpeg ----"
which -s ffmpeg || /usr/local/bin/brew install ffmpeg
The problem I am facing is when Homebrew is installed but in a non-standard location.
I have to use the full path to Homebrew because this script is being run within Playtypus.
So the question is : How can I reliably get the installed path of Homebrew in a BASH script?
Answering my own question...
You can test the output of which brew and deal with things accordingly. To gracefully deal with the case where Homebrew is not installed you can use if which brew 2> /dev/null which redirects stderr to /dev/null.
brew --prefix is also useful here as it give the path to where Homebrew installed applications are symlinked to, rather than their actual install path.
A script which works and shows this working :
#!/bin/bash
if which brew 2> /dev/null; then
brewLocation=`which brew`
appLocation=`brew --prefix`
echo "Homebrew is installed in $brewLocation"
echo "Homebrew apps are run from $appLocation"
else
echo "Can't find Homebrew"
echo "To install it open a Terminal window and type :"
echo /usr/bin/ruby -e \"\$\(curl\ \-fsSL\ https\:\/\/raw\.github\.com\/Homebrew\/homebrew\/go\/install\)\"
fi
Thanks to Allendar for the pointers.
Just to add to this, Homebrew's --prefix mode has been enhanced here in the far-flung future of 2020 (or maybe it was always this way), so that it now takes a package name as an argument. Meaning locating those "keg-only" packages which aren't linked into standard paths is as easy as:
$ brew --prefix ffmpeg
/usr/local/opt/ffmpeg

Redirecting bash script to /dev/null not executing?

so I'm very new to bash and I was making an installer for build-essential and OpenSSL. The problem is that It always stops after the first exec line. Here's my code:
#!/bin/bash
echo "Installing build-essential"
exec sudo apt-get install build-essential > /dev/null 2>&1
echo "Finished installing build-essential"
echo ""
echo "Installing OpenSSL"
exec sudo apt-get install openssl > /dev/null 2>&1
echo "Finished installing OpenSSL"
echo ""
echo "Updates complete!"
And here's the output:
Installing build-essential
[sudo] password for matthew:
Please keep in mind that I just started a few hours ago. Sorry for the dump question.
exec never returns to the calling script. It replaces the current process with the command following exec. Just remove the exec altogether, and let apt-get run like any other command.
Note: there are uses of exec that do return to the calling script, such as those that only do I/O redirection.

Resources