Is It OK to Run Shell Scripts Like This?

I have never really understood shell or $PATH and it's related ENV_PATH variables etc. Is it OK to run shell scripts like this?

It seems to do what I want it to do, I just don't know if there are unseen consequences for writing it this way, like macro run speed, computer performance, etc.

Thank you!

It looks ok. You may want to specify the full path to the id3tag utility.

Other than that, if it works ok, it looks ok.

1 Like

Hey Christian,

$PATH is just a variable that contains a list of directories where the shell looks for Unix executables when they are called directly by name (without a full path).

Type echo $PATH into the Terminal.app and hit Return – you'll end up with a list of directory paths that looks similar to this

/Users/someUserName/Library/Python/3.9/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The colon character is actually a separator between each separate directory path.

/Users/someUserName/Library/Python/3.9/bin
/opt/local/bin
/opt/local/sbin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Each of these defined directory paths is in order where the system looks for Unix executables when you call them by name.

Since I have /opt/local/bin defined in the $PATH I can call a 3rd party executable called cliclick directly by name instead of invoking its full path.

/opt/local/bin/cliclick <-- This is cumbersome....

One thing you have to remember about running Unix executables from Keyboard Maestro is that KM's environment is NOT the same as macOS'.

So any changes you've made to the system are NOT automatically reflected in KM.

To make KM have the same $PATH as the system I have to use KM's environment variable ENV_PATH and give it the same value as my macOS system $PATH. That way KM's shell environment knows where to look for any executables that I might call directly.

It seems a bit complex, but if you understand file/folder path strings you've already got much of what you need to understand $PATH.

-Chris

3 Likes

While a bit off-topic, and realising this may be only an example, your biggest speed hit is repeatedly evaluating podcastTitle. You aren't using a recursive "For Each..." so the value is the same for every loop.

Try putting the target directory into a variable and extract the title before the loop -- that'll also do away with regex action. So something like:

image

(Yes, I'm continuing my crusade for variable scoping. Using Local__ variables makes things more unwieldy in the shell script, but will prevent problems if two instances of this are run simultaneously. If you do need Globals then include "Semaphore Lock" actions as appropriate.)

1 Like

This is easily managed by converting the $KMVAR_local_VarName form into a standard shell variable at the beginning of your script.

This technique has the benefit of encouraging users to actually test their variables to make sure they work before moving on to flesh out the rest of the script – and that squashes a whole lot of bugs before they hatch.

Scripts become easier to read and easier to debug.

** Note that I'm using a shebang line, because my preferred shell is not the default.

Local Variables in the Shell.kmmacros (2.2 KB)
Keyboard Maestro Export

1 Like