Getting KM to use a specific shell containing the right ruby / gems

Any time I try to execute a ruby script that requires certain gems, I get errors like this:

Ignoring bigdecimal-1.2.7 because its extensions are not built. Try: gem pristine bigdecimal --version 1.2.7 Ignoring io-console-0.4.5 because its extensions are not built. Try: gem pristine io-console --version 0.4.5 Ignoring libxml-ruby-2.8.0 because its extensions are not built. Try: gem pristine libxml-ruby --version 2.8.0 Ignoring nokogiri-1.6.7.2 because its extensions are not built. Try: gem pristine nokogiri --version 1.6.7.2 Ignoring psych-2.0.17 because its extensions are not built. Try: gem pristine psych --version 2.0.17 Ignoring rmagick-2.15.4 because its extensions are not built. Try: gem pristine rmagick --version 2.15.4 Ignoring sqlite3-1.3.11 because its extensions are not built. Try: gem pristine sqlite3 --version 1.3.11 Ignoring unf_ext-0.0.7.2 because its extensions are not built. Try: gem pristine unf_ext --version 0.0.7.2 Ignoring websocket-driver-0.6.3 because its extensions are not built. Try: gem pristine websocket-driver --version 0.6.3 /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:inrequire’: cannot load such file – rmagick (LoadError)
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in require' from /Users/Greg/Projects/Ruby/RubyScripts/getSide.rb:2:in'`

Typically I would make sure that the right instance of ruby is running using a tool called ‘rvm’ (ruby version manager).

But when I try to use rvm, I get this:
/var/folders/tl/hrcdxt0s2ts9nb99kjmz11zc0000gn/T/Keyboard-Maestro-Script-922F95AB-2406-4940-B506-AC5834D87EC8: line 1: rvm: command not found

So now I have to ask - what shell is ruby using and how do I make it use the same shell as if I opened Terminal?

I realize I could switch to Terminal using KM and input the script command manually - however, I want to keep this in the background so that’s not an option.

Hey Gregory,

See “How Scripts Are Processed” on this page in the wiki:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

In short:

NONE of your terminal environment is picked up from any config files by the shell Keyboard Maestro invokes.

You can get around this by using the `source` command.

#!/usr/bin/env bash
source ~/.profile;

-Chris

4 Likes

Chris, thanks for this great tip.

Would you mind providing more information about this technique, like when it is appropriate to use it, beyond the use case that Gregory posted?

Would it hurt anything to always use it, just to be safe?

Also, I read the superuser.com article, but thought I’d post this output from the “help source” command:

source: source filename [arguments]
Read and execute commands from FILENAME and return. The pathnames
in $PATH are used to find the directory containing FILENAME. If any
ARGUMENTS are supplied, they become the positional parameters when
FILENAME is executed.

[quote="JMichaelTX, post:3, topic:3645"]
Would you mind providing more information about this technique, like when it is appropriate to use it, beyond the use case that Gregory posted?[/quote]

Hey JM,

If you read the superuser article you know nearly as much as I do.   :smile:

I wouldn't. It adds complexity, overhead, and you never know how various installed software will alter your ~/.profile or ~/.bash_profile or ~/.bashrc files.

To be safe I'd use the bare shell Keyboard Maestro defaults to unless I had a genuine need to do otherwise.

Not that I'd be overly afraid to source my ~/.profile file, but I'd probably be a little more careful about its hygiene.

The source command executes statements in the referenced file:

The only line I have in this test file is:

cd ~/Downloads;

So the output of pwd (print working directory) is as follows:

If I was going to use a .config file on a regular basis in my Keyboard Maestro shell scripts, I would probably use a custom config file and not the normal ~/.profile file I use with Terminal.

I say all this as an inexpert user of the shell. While I use it quite a lot, I'm still an intermediate-level user. I may change my mind at some point.

However. My current opinion is also based on almost 40 years of using computers and seeing situations where custom configurations cause issues (as a user who likes to customize everything).

A little more reading material on the subject:

http://wiki.bash-hackers.org/howto/conffile

-Chris

2 Likes

Awesome.

The only thing I would add is to make sure to place all of your intended code in the same execute shell:

Correct:

Incorrect: