Path to excutables in shell scripts

In many of my macros I use shell scripts.
Many tools are installed through Homebrew.

But then when I update it the path to the executable changes.
And since I need to reference this directly in KM, the macros stop working.

An example of a shell script is this:

/usr/local/Cellar/imagemagick/6.9.0-10/bin/convert /tmp/compare/Diff_$KMVAR_CompareRunTimes.jpg /tmp/compare/$KMVAR_ComparePDFname

So when ImageMagick is upgrade I need to update the path.

Does anybody has a tip to avoid this?

Is there not a link from a non-versioned path somewhere?

Like /usr/local/Cellar/imagemagick/current/bin 

or some such?

Alternatively, how is the path set in your shell? Write your Keyboard Maestro macro to source the same shell setting file first, or read the path from somewhere and use it within the command. Or, of course, if only one version is installed, you could use:

/usr/local/Cellar/imagemagick/*/bin/convert

I have a path

usr/local/bin

where the executables are as links.

But I was under the impression that links would not work.
I will use this folder from now on.
Thanks

1 Like

Hey Jimmy,

Run this from a Keyboard Maestro Execute Shell Script Action and display text.

echo $PATH

You'll see something like this:

/usr/bin:/bin:/usr/sbin:/sbin

Quite obviously it does not contain your path /usr/local/bin/…

Keyboard Maestro ignores your bash profile or .profile configuration when it instantiates a shell (as does AppleScript).

You can hardcode to that path in your command:

/usr/local/bin/compare

But the better way in my opinion is to adjust the path as needed in your script thus:

export PATH=whatever/you/want:$PATH

In your case:

export PATH=/usr/local/bin:$PATH;

That way you don't have to write the entire path to the command, and your code reads much easier.

-Chris

3 Likes

Hi Chris

I am a bit of a n00b in regards to shell scripts.

I have bought this book: http://www.takecontrolbooks.com/command-line?pt=TB1270
Which I hope helps with this.

But in this regard, then my shell script should look like this.

export PATH=/usr/local/bin:$PATH;
convert /tmp/compare/Diff_$KMVAR_CompareRunTimes.jpg /tmp/compare/$KMVAR_ComparePDFname

And I can confirm that it works perfectly.

1 Like

Hey Jimmy,

Good deal.

We're all noobs sometime. :smile:

I bit the bullet several years ago and started wrestling with sed and awk.

It made me buy a book. I then bought "Beginning Shell Scripting".

I'm still far from expert.

Search Google for link.

Look for the first pdf in the list.

-Chris

The TidBITS book, Take Control of the Mac Command Line with Terminal may be useful for getting started.