1

I've created a ruby script that sets up a new mac.

Among other things it creates a .bash_profile, .gitconfig and configures various system settings such as displaying the full POSIX path as the Finder window title (super useful).

Mostly I'm running commands in backticks such as `defaults write com.apple.finder _FXShowPosixPathInTitle -bool true` the aforementioned full POSIX path as the Finder window title trick.

All this works just fine.

What I want to do is have this ruby script run the Homebrew installer too. The bash command for this is :

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

However this doesn't work when called using backticks.

So my question is how do I run another ruby script (which the Homebrew installer is) from within a ruby script?

And more specifically how would I kick off the web based interactive Homebrew installer (well you have to press return at least once) from within a ruby script and for it's output to show in the terminal?

I know that I could rewrite this all as bash script but I'd really rather keep it all within ruby.

1 Answer 1

2

Let's decompose what $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" actually does:

  1. download, via curl, the homebrew install ruby file. Since the command is surrounded by $(), it executes the command and passes the output to ruby.
  2. execute the script via Ruby. The -e flag instructs Ruby to execute the script from the command line instead of loading a specified file.

Since we know that it's a ruby script, we can just do the following:

  1. using Net::HTTP or some other ruby library, download, the homebrew install file.
  2. eval() or otherwise execute the homebrew ruby script.

Of course, eval() is dangerous, especially with untrusted input, but you're already essentially running eval on the script anyways with the install command provided.

In script form that would be:

require 'net/http'

homebrew_uri = URI('https://raw.githubusercontent.com/Homebrew/install/master/install')
homebrew_script = Net::HTTP.get(homebrew_uri)
eval(homebrew_script)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.