从命令行检测 Apple Silicon

Kla*_*urn 9 bash shell macos-big-sur apple-silicon apple-m1

如何从 shell 脚本中检测到它在 M1 Apple 硬件上运行?

我希望能够运行命令行命令,以便我可以编写一个if-statement,它的主体只会在带有 M1 处理器的 mac 上运行时才会被执行(自然至少是 macOS Bug Sur)。

MBa*_*aas 30

我发现即使该过程是在 Rosetta 下运行的,也sysctl -n machdep.cpu.brand_string有报告。Apple M1

更新:做好准备Apple M1 ProApple M2等等Apple M2 Max

  • 这是干净地回答“我可以通过‘arch -arm64 bash’离开这里吗?”所需要的。很难相信“arch”没有“-l”来列出当前机器可用的体系结构。 (3认同)

bal*_*ton 12

当使用本机 shell/bin/bash -i或时/bin/zsh -iKlas Mellbourn 的 答案按预期工作。

如果使用通过 Intel/Rosetta Homebrew 安装安装的 shell,则uname -p返回i386,并uname -m返回,如Datasun 的注释x86_64所示。


为了获得跨环境(Apple Silicon Native、Rosetta Shell、Linux、Raspberry Pi 4s)工作的东西,我使用了dorothy dotfile 生态系统中的以下内容:

is-mac && test "$(get-arch)" = 'a64'
Run Code Online (Sandbox Code Playgroud)

如果您不使用 dorothy,则 dorothy 的相关代码是:

https://github.com/bevry/dorothy/blob/1c747c0fa6bb3e6c18cdc9bae17ab66c0603d788/commands/is-mac

test "$(uname -s)" = "Darwin"
Run Code Online (Sandbox Code Playgroud)

https://github.com/bevry/dorothy/blob/1c747c0fa6bb3e6c18cdc9bae17ab66c0603d788/commands/get-arch

arch="$(uname -m)"  # -i is only linux, -m is linux and apple
if [[ "$arch" = x86_64* ]]; then
    if [[ "$(uname -a)" = *ARM64* ]]; then
        echo 'a64'
    else
        echo 'x64'
    fi
elif [[ "$arch" = i*86 ]]; then
    echo 'x32'
elif [[ "$arch" = arm* ]]; then
    echo 'a32'
elif test "$arch" = aarch64; then
    echo 'a64'
else
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

Jatin Mehrotra重复问题的回答提供了有关如何获取特定 CPU 而不是架构的详细信息。在我的 M1 Mac Mini 上使用输出,但在 Raspberry Pi 4 Ubuntu 服务器上输出以下内容:sysctl -n machdep.cpu.brand_stringApple M1

> sysctl -n machdep.cpu.brand_string
Command 'sysctl' is available in the following places
 * /sbin/sysctl
 * /usr/sbin/sysctl
The command could not be located because '/sbin:/usr/sbin' is not included in the PATH environment variable.
This is most likely caused by the lack of administrative privileges associated with your user account.
sysctl: command not found

> sudo sysctl -n machdep.cpu.brand_string
sysctl: cannot stat /proc/sys/machdep/cpu/brand_string: No such file or directory
Run Code Online (Sandbox Code Playgroud)

  • 未选中“使用 Rosetta 打开”: https://www.dropbox.com/s/lrhxgkxtmi2wv74/CleanShot%202021-06-28%20at%2006.24.52%402x.png?dl=0 如果我运行新命令 ` /bin/bash -i` 然后运行 ​​`uname -m` 然后它返回 `arm64` 并且 `uname -p` 返回 `arm`。这是由我的默认 shell 引起的,它是由 homebrew 安装的,通过 Rosetta 运行。感谢您帮助我查明此事。我已经更新了我的答案。 (2认同)

Kla*_*urn 11

uname -m
Run Code Online (Sandbox Code Playgroud)

将返回arm64而不是x86_64

if [[ `uname -m` == 'arm64' ]]; then
  echo M1
fi
Run Code Online (Sandbox Code Playgroud)

或者,正如@chepner 建议的那样

uname -p
Run Code Online (Sandbox Code Playgroud)

将返回arm而不是i386

if [[ $(uname -p) == 'arm' ]]; then
  echo M1
fi
Run Code Online (Sandbox Code Playgroud)

  • 不仅是在 Rosetta 中运行终端,而且运行脚本的任何进程是否也恰好在 Rosetta 中运行。如果有人在由(例如通过 Rosetta 运行的 RMM)部署的脚本中运行此命令,您将获得“x86_64”。我认为 `sysctl` 方法是在字符串中搜索“Apple”的最佳解决方案:`[[ $(sysctl -n machdep.cpu.brand_string) =~ "Apple" ]]` (9认同)
  • 请注意,M1 用户可以在 Rosetta 模式下运行终端。在这种情况下,“uname -m”返回“x86_64”。 (7认同)