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 Pro,Apple M2等等Apple M2 Max !
bal*_*ton 12
当使用本机 shell/bin/bash -i或时/bin/zsh -i,Klas 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)
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)