Howdy folks, hoping to get some advice here on setting the ENV_PATH variable for multiple devices and scripts.
For instance, I run a variety of shell scripts on my iMac, both OS scripts and home brew scripts, as well as on my wife's MacBook, also OS and home brew. My iMac is running Big Sur, while the MacBook is running Monterey.
The path for OS shell scripts is apparently different for the two OS (no surprise), so initially I was just running a subroutine before any shell script action that would set the variable according to the device's UUID to differentiate between my iMac and her MacBook. However, now that I run home brew scripts on both devices, it has added a level of complexity in defining where the heck the proper path is for any given shell script.
So I'm hoping to get some ideas as to the best way to set the ENV_PATH variable depending on what device and what kind of script I'm running. I was thinking of having the subroutine first use a switch action to ID which device I'm on using the same %MacUUID% token I'm currently using, and then inside of that switch action, another switch that will run according to the %TriggerValue% token. For instance, I could supply the token of homebrew to indicate that the script is a homebrew script and needs to use that path, and leave the parameter empty to run OS scripts.
But since I'm still relatively new at running shell scripts, and don't want to screw anything up I'm hoping some of the more knowledgable folks can chime in with their ideas. Any help is appreciated!
Here’s how I came up with an ENV_PATH to work on all my computers:
On each computer, run
echo $PATH | sed 's/:/\n/g'
This will give you list of the directories that are searched for executables on that computer. The sed part isn’t necessary, but it breaks the list up onto separate lines, which makes the list easier to read.
Looking at lists from all your computers, assemble a new list that is a superset of all the directories. In this list, the directories must be separated by colons, not newlines.
Save this new list as ENV_PATH in Keyboard Maestro on all your computers.
The order of the list matters, because the directories are searched in that order. So if you’ve installed a version of, say, Perl that’s more recent than the one that comes with the Mac, you’ll want to make sure the directory it’s in comes before /usr/bin.
Don’t worry if the list contains directories that don’t exist on all the computers. When the system encounters a non-existing directory, it just moves on to the next one in the list.
This is awesome! I just ran it on my iMac and got the exact list I had initially set my variable to earlier this year when I started working with shell scripts. Later on today I'll run it on the MacBook and update my variable, thanks so much!
The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline character in
an address or in the substitute command.
sed might be overkill anyway Try echo $PATH | tr ':' '\n' -- it should work on both (all?) platforms too, so no need for the "If Then Else".
thank you for providing this less complex example using the tr - command I had the man page in front of me yesterday because a family member pointed me to it saying "its worth to have this thing in your Toolbox for Shell scripting"....
I tested your approach with the piped tr command on both my Macs - it just works...
now to the piped sed command: how could I fix the issue using sed ??
I want to know this for learning.
It'll work if you use the "old" version above -- echo $PATH | sed $'s/:/\\\n/g'. That also works on Monterey, so it's the most portable sed version. Perhaps the "new" style that @drdrang offered came in with Big Sur, or maybe Monterey...
I'm afraid I don't know enough about how sed works to tell you why, but it must be something to do with explicitly setting the "address" to the last line of input (the $ in the command). I can glean that much from the man page -- but haven't a clue what it means!
thanks for pointing me in the right Direction - now I think that I better understand the two sed approaches..... the "old" style - originally posted here by @ccstone is the most common Version which will work on the most Systems out of the box - even many newer systems - because of the backwards compatibility for sed using that command only written in the old style to get every $PATH - Directory from the System as a List.
The Version from @drdrang will only work - if you have a macOS Version that also delivers the ability for the "Newer" Style with the updated/enhanced Version of sed. - And in this particular case this could be like you said Big Sur or Monterey.
Damn it - I knew it wasn't a good idea yesterday writing that Macro I posted above on a hurry....
Thank you so much Nige - you made this more clear to me - perhaps someone with more knowledge about the "Newer" Style will write here about it - that would be great but for now I hopefully have everything I need to know and everything I have written in this message is right.
Tobias - I can't really answer the question regarding different versions of sed but I can give you an alternative way to get the PATH environment variable as a list on any Mac with KM (which is the way I've been doing it for a while now...)