I'm a newbie at using the shell. I installed a tool called csvkit to help with some of my tasks. I'm using a command named csvstat. It works fine from the Terminal, but KM doesn't seem to understand the command. It returns this error:
/var/folders/w_/1s9d7ph56lx9dvbz_1q2bj9r0000gn/T/Keyboard-Maestro-Script-23194AB1-19B5-4F4F-890C-60843371A853: line 1: csvstat: command not found
I know the wiki gives the following disclaimer—maybe this is relevant?
Shells are executed in non-interactive mode … . As such none of your profile scripts (like ~/.profile) will be executed. This means that custom environment variable settings will not be applied, in particular any settings for your PATH environment variable, and any settings for tool-specific environment variables like PERL5LIB). You can set these by creating a variable with “ENV_” at the start (eg ENV_PATH or ENV_PERL5LIB) and settings its value appropriately.
you can use "which" or "whereis" in terminal to locate shell files. Unfortunately I don't find a file of thet name OMM.
john-iMac:~ john$ which csvstat
john-iMac:~ john$ whereis csvstat
john-iMac:~ john$ which ls
/bin/ls
john-iMac:~ john$ which textutil
/usr/bin/textutil
I had found the files on GitHub, but the relevant pages didn't give me a satisfactorily clear method of installation – particularly since pip is not installed by default on OSX.
So I had to figure out how to install pip, before I could continue. With that accomplished it was easy to install csvkit, and with csvkit installed it became easy to test.
@ccstone Wow, I’m impressed! Thanks so much for doing that extra sleuthing, and sorry I wasn’t clearer before. Yes, that’s solved it.
How did you end up finding csvkit after you had installed pip? With whereis?
Also, can you explain the #!/usr/bin/env bash -l line? It looks like you are simply telling bash to run with my login environment—in which case it doesn’t matter where csvkit is—right?
whereis checks the standard binary directories for the specified programs.
which searches the path for each executable file that would be run had the command actually been invoked. (Very important when searching for user-installed items.)
which csvstat
Precisely.
Right.
Since your log-in environment alters the stock $PATH to include your Python stuff.
Thanks! This is extremely helpful. So if an installed tool is always accessible from my login environment, then I can get to it in KM with #!/usr/bin/env bash -l, right?
Personally I don't like to pollute my Keyboard Maestro environment with stuff from my log-in environment, because of the potential for unintended side-effects – so I only use that sort of thing when it's needed.
When I don't need to pick up environment variables, aliases, or functions from my custom log-in files but do need items outside of the default $PATH structure I do one of a couple of things:
A) You can set Keyboard Maestro's environment variables the Editor's variable preferences:
As @ccstone has alluded to, its not generally a good idea to use bash -l. It might get you working, but long term in may also be unreliable. There is a reason that your login scripts and such are not read automatically when you run scripts, because they change the environment in unpredictable ways. For example, you might think later it is a good idea to have your bash login print out a fortune or the date or some status information (maybe your harddisk free space for example). All good ideas, but then your script running with bash -l now gets strange extra output that is not expected.
So while using bash -l to quickly check that things can be made to work might be ok, better is then to avoid it and configure just the required environment settings appropriately.
Thanks, @peternlewis and @ccstone. This is good to know. I see why I might not want all the (potential) cruft of my bash login to transfer to my KM scripts.
Could either of you explain a little bit more what it means to change the environment variables? It looks like I’m telling the shell, “please pick up any installations I might have in the following folders….” Is that correct?
And am I correct in thinking that my having used python for the csvkit tool complicates the matter because we’re not sure where that was installed?
Open the Terminal, and type “env” return. It will show you what the environment looks like in the Terminal, with your login options set. There will be options in there that affect the running of commands, such as the PATH environment variable which controls where the system will look for commands that don’t have a full path. There may be other environment variables that affect the tools that you use, for example the PERL5LIB environment variable controls how the perl tool will search for libraries.
You can set all the applicable environment variables in your Execute Script action, before executing the command you want to perform, or alternatively, you can set them in the Keyboard Maestro variables by adding “ENV_” to the front of the name, for example the Keyboard Maestro ENV_PATH will be used to set the PATH environment variable before executing scripts.
You cannot do that by setting a Keyboard Maestro variable - it is all or nothing.
You can do that within the Execute Shell Script action at the start by setting the PATH appropriately, and using the PATH as part of the value you set (in csh, it is setenv PATH /opt/local/bin:/opt/local/sbin:$PATH, I’m not sure about bash scripts).