Friday, May 27, 2022

#1 2022-05-06 03:06:36 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 545

Finding location of shell commands on an M1

We use a lot of shell utilities installed via Homebrew and called from Applescripts.

Since the shell command run from Applescript doesn't assume the same relative paths as for a user, many of these aren't being run from simply calling the command:

redis-cli [command]


but from their path:

/usr/local/bin/redis-cli [command]


But on M1 Macs, Homebrew uses completely different paths for everything, so our existing scripts fail.
https://docs.brew.sh/Installation
We've actually had this off an on before with default paths for packages changing, or users having something installed some way other than Homebrew defaults. But we've just fixed things on a case-by-case basis.

So I thought I could use a tool like "which" or "path_helper" to find my Homebrew tools for me, and hopefully have an elegant solution that will continue to work if paths change on me again.

In terminal on an M1, either of these will return the path to a Homebrew application:

which redis-cli


"eval `/usr/libexec/path_helper -s`; which redis-cli"


However, inside a "do shell script," neither of those works.
Both return:

error "The command exited with a non-zero status." number 1


On an Intel Mac, the "path_helper" one works from a "do shell script."

The "which" command fails in the same way from inside a "do shell script" on an Intel Mac...
On the Intel, in Terminal I can run:

which which


and it returns:

/usr/bin/which


Yet running

Applescript:

do shell script "/usr/bin/which redis-cli"

still fails.
On the M1, running

which which


in Terminal just returns:

which: shell built-in command


So even if I got it working on Intel, sounds like it's not going to work on M1.

I should probably just give up and have a function that goes and looks for each Homebrew app at each known path we've seen it at and returns the path for wherever it found it.

But I'm very curious what's going on here, and if there's any way to use a tool like "which" or "path_helper" to find these tools for the generic case.

Thanks in advance for any help. I realize a lot of you won't have M1's to test on, but maybe you can at least help explain how this works/doesn't work.

EDIT:
I noticed that Apple changed the default shell from bash (or bash emulating sh?) to zsh.

This https://unix.stackexchange.com/question … er-and-zsh claims that path_helper doesn't work with zsh.

So I tried:

Applescript:

do shell script "/bin/bash -s <<'EOF'
eval `/usr/libexec/path_helper -s`; which redis-cli
EOF"

But I got the same thing:

error "The command exited with a non-zero status." number 1

Last edited by t.spoon (2022-05-06 03:09:01 pm)


Ditched the Hackintosh.
Intel Mac Mini i7 and M1 Mac Mini on a KVM.

Offline

 

#2 2022-05-07 01:46:36 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 199

Re: Finding location of shell commands on an M1

Looking at the man page for an older version of brew, you could try prefix. Maybe it will work similarly on an M1.

$ brew --prefix
--> /usr/local

From the man page: Display Homebrew ́s install path. Default: /usr/local on macOS and /home/linuxbrew/.linuxbrew on Linux

If you add the formula then you should get the equivalent of 'which'.

% brew --prefix xdelta
--> /usr/local/opt/xdelta

Offline

 

#3 2022-05-08 01:14:20 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 956

Re: Finding location of shell commands on an M1

In unix its common to have variables in shell configuration file ex. bash_profile in your user home directory.

This example show 2 do shell script with different PATH

Applescript:

log (do shell script "echo $PATH")

set homeFolder to POSIX path of (path to home folder from user domain)
log (do shell script "source " & homeFolder & "/.bash_profile; echo $PATH")

If you have the brew install path as variable and then you could use unix command: source

PATH=/opt/homebrew/bin:$PATH

Last edited by Fredrik71 (2022-05-08 01:29:02 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)