1

I'm trying to create a script to deploy macOS and the last piece that isn't automated is installing Homebrew. Because of the unique scenario where Homebrew shouldn't be installed with sudo, and it requires both the return key to be pressed and the users password, it's become a tricky thing to figure out. I've spent a few hours on this and found other unanswered questions on StackExchange with no luck on getting it completely automated.

My code:

echo "Password: "
read -s PASSWORD
echo $PASSWORD | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The echo skips the request to hit return but doesn't pass in the password because I'm missing the -S flag because I shouldn't include sudo when installing Homebrew. I ultimately need a setup that hits command AND passes in the password without using sudo. The rest of my script runs by using echo $PASSWORD | sudo -S my-command-here... so I'd like to not deviate too far from that scheme if possible.

4
  • Some more details might help. What exactly is the problem you are trying to solve here (your question mainly shows your attempts to solve it, but not really what is the problem in the first place). Is this script running as an admin user? Can you change it in a way which ensures that the whole script runs as an admin user? Can you create and chmod /usr/local before you install Homebrew?
    – nohillside
    Commented Aug 29, 2019 at 6:24
  • I added an edit last night that hopefully provides some additional details. It doesn’t look like it’s been approved yet. The command exits if you try running it as sudo. I suppose we could create and chmod the folder - only if it’s impossible to have the script press return and pass the password through in a single command though. Stay posted for that edit to hit and it should help add info. Commented Aug 29, 2019 at 13:43
  • You added it to the answer instead of the question. Please edit the question instead.
    – nohillside
    Commented Aug 29, 2019 at 13:59
  • Ignoring for a momentan that using echo PASSWORD | sudo -S is a bad practice (at least from a security point of view) your problem seems to come from the fact that sudo gets called without -S within the Homebrew install script. So you may want to ask for ways to prevent Homebrew from even trying to use sudo (don't know whether this is possible) or for ways to change your script to work around this.
    – nohillside
    Commented Aug 29, 2019 at 14:10

1 Answer 1

1

The -s option is working for me, so I don't know why it doesn't work for you.

Have you tried to add #!/bin/sh (or #!/bin/bash) in the top of your script ??

If you use the -n option on echo you'll get the input on the same line as the label.

So the below is having the input: Password: <waiting for input>, and on the next line it just print the password entered - the awk is just for testing that the pipe was working. And then I've added a loop so that something always need to be entered.

#!/bin/sh

readPassword
echo $PASSWORD | awk '{print $1}'

readPassword() {
   echo -n "Password: "
   read -s PASSWORD
   echo ""
   if [[ -z "$PASSWORD" ]]; then
      printf '%s\n' "A password is required..."
      readPassword
   fi
}
3
  • Yes, this is a small portion of my script (the only piece not working without interaction from the user) and I've included the #!/bin/bash at the top of the real script. The -n flag is a nice addition thanks! However, this doesn't really fix the Homebrew installation problem in my initial question. I've got other commands such as echo $PASSWORD | sudo -S systemsetup -setremotelogin on to work with the -S flag but I have to include sudo to get them working. Homebrew shouldn't be installed via sudo so this setup doesn't work unless I'm missing something. Commented Aug 17, 2019 at 21:54
  • Maybe I don't understand your real problem. You don't need a password for installing Homebrew (your sample in the question), so why do you want to add a password ?? If a command doesn't use parameters that are passed to it, then you don't need to pass anything to the command - like here with a password, that the command doesn't use. Commented Aug 18, 2019 at 9:04
  • My question is similar to the question asked here: stackoverflow.com/questions/24426424/…. This question's answers don't work though as the script will still prompt for the password. My complete script: github.com/Justintime50/mac-scripting/blob/master/deploy/nozani/… runs with me only needing to type the password once at the very beginning except for Homebrew where I need to have the script both hit return for me and type in the password at the prompt. Installing Homebrew does ask for a password. Commented Aug 19, 2019 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .