Installing brew from macOS app's pre/postinstall script - bash

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:

Related

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

installing oh-my-zsh for a different user as root in cloud-init script

I'm attempting to bootstrap my AWS EC2 ubuntu server with oh-my-zsh installed for the ubuntu user. I have a cloud-init script (more info here) that runs as the root user (with sudo). So, in my script I run the oh-my-zsh installation as the ubuntu user.
#cloud-config
runcmd:
# omitted other commands specific to my server, install zsh at the end
- apt-get install -y zsh
- su ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
# change the prompt to include the server hostname
- su ubuntu -c echo "echo export PROMPT=\''%{$fg[green]%}%n#%{$fg[green]%}%m%{$reset_color%} ${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'\'" >> /home/ubuntu/.zshrc
# get environment variables defined above
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
When the cloud-init finishes and I ssh into the colors are not working in the $PROMPT, I see [green] and [cyan]:
[green]ubuntu#[green]ip-172-31-27-24 [cyan]~
If I run the same PROMPT command as the ubuntu user after ssh'ing in, the colors work correctly:
The problem seems to be how the colors are evaulated when the cloud-init script runs the echo command vs how the colors are evaulated when the ubuntu user runs the echo command. Does anyone know how I might change the PROMPT so the colors are only evaulated once ~/.zshrc is evaulated by the ubuntu user?
I solved this thanks to jgshawkey's answer here. I used bash variables to escape the color codes & commands to postpone their evaluation:
- apt-get install -y zsh
- runuser -l ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
- fgGreen='%{$fg[green]%}'
- fgCyan='%{$fg[cyan]%}'
- fgReset='%{$reset_color%}'
- retStatus='${ret_status}'
- gitInfo='$(git_prompt_info)'
- runuser -l ubuntu -c "echo export PROMPT=\''${fgGreen}%n#%m${fgReset} ${retStatus} ${fgCyan}%c${fgReset} ${gitInfo}'\'" >> /home/ubuntu/.zshrc
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
It ended up looking like this in my ~/.zshrc:
Since I am creating a new user/server, I made this way:
user=you user here
pass=you password here
apt install -y zsh curl wget # considering you currently are root
#creates a new user with password and zsh as default shell
useradd "$user" -m -p $(openssl passwd -1 "$pass") -s $(which zsh)
usermod -aG sudo "$user" # append to sudo and user group (optional)
# mind the command above, with the parameter --unattended which means "no questions" and "no set default to zsh"
runuser -l $user -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended'
Worked fine for me.

How to check if command exists for a user?

I install Pip for a user (not system wide) and I would like to check that pip is installed for that user in my script that I run with sudo: sudo ./script.sh
I know to check for a command with command -v pip3 and that works when I enter it in the shell as the user.
But how can I check it in my script?
command -v pip3 exit code is 1 because I am root (because of sudo).
su -c "command -v pip3" "$SUDO_USER" has exit code 1.
sudo -u "$SUDO_USER" command -v pip3 says "command: command not found"
The simplest is
sudo -u "$SUDO_USER" -i command -v pip3
The -i option causes sudo to pass the supplied command line to the user's configured shell using its -c option, instead of trying to execute the command directly. That's necessary because command is a shell built-in; it doesn't exist as a stand-alone executable. (The -i options runs a "login" shell. There is also the -s option which runs a non-login shell. See below.)
If you want to specify a shell explicitly you could do so instead:
sudo -u "$SUDO_USER" /bin/sh -lc "command -v pip3"
Again, a login shell is forced, here by using the -l option.
As a safety feature, sudo normally resets the $PATH to a "safe" value before executing the shell (or the single command). That value will not have any of the modifications made in the /etc/profile and ~/.profile startup scripts, and without those modifications -- which add one or more user-specific directories to the path -- the shell will not find software such as pip3 which has been installed for individual users.
use following command by replacing $USER with the specific user name.
sudo -H -u $USER bash -c 'command -v pip3'
similarly, you can run any command as another user
syntax : sudo -H -u $USER bash -c 'INSERT_COMMAND_HERE'

Unable to install parse.com command line tool on Mac OSX 10.10 Yosemite

Running the command
curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash
does not install the tool
I was able to install it easily on my other computer running 10.9.2
STEP : 1
Make a copy of this
#!/bin/bash
TMP_FILE=/tmp/parse.tmp
if [ -e /tmp/parse.tmp ]; then
echo "Cleaning up from previous install failure"
rm -f /tmp/parse.tmp
fi
echo "Fetching latest version ..."
curl --progress-bar https://www.parse.com/downloads/cloud_code/parse -o /tmp/parse.tmp
if [ ! -d /usr/local/bin ]; then
echo "Making /usr/local/bin"
mkdir -p /usr/local/bin
fi
echo "Installing ..."
mv /tmp/parse.tmp /usr/local/bin/parse
chmod 755 /usr/local/bin/parse `
to a file named install.sh and run it in your terminal as bash install.sh. This will install you parse in your Terminal.
STEP :2
Download the Gist from this link and run the file named install.sh in your Terminal preceded by bash

Unattended (no-prompt) Homebrew installation using expect

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

Resources