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
> 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
3条答案
按热度按时间ltqd579y1#
将返回
arm64
,而不是x86_64
或者,就像@chepner建议的那样
将返回
arm
,而不是i386
另一个工具是
arch
:xqk2d5yq2#
我发现
sysctl -n machdep.cpu.brand_string
报告了Apple M1
,即使进程是在Rosetta下运行的。更新:准备好
Apple M1 Pro
、Apple M2
、Apple M2 Max
等!vhmi4jdf3#
当使用原生shell(比如
/bin/bash -i
或/bin/zsh -i
)时,Klas Mellbourn'sanswer可以按预期工作。如果使用通过Intel/Rosetta Homebrew安装的shell,则
uname -p
返回i386
,uname -m
返回x86_64
,如Datasun's注解所示。为了获得跨环境(Apple Silicon Native,Rosetta Shell,Linux,Raspberry Pi 4s)工作的东西,我使用dorothy dotfile ecosystem中的以下内容:
如果你没有使用dorothy,dorothy的相关代码是:
https://github.com/bevry/dorothy/blob/1c747c0fa6bb3e6c18cdc9bae17ab66c0603d788/commands/is-mac
https://github.com/bevry/dorothy/blob/1c747c0fa6bb3e6c18cdc9bae17ab66c0603d788/commands/get-arch
a duplicate question上的Jatin Mehrotra'sanswer详细介绍了如何获取特定的CPU而不是架构。在我的M1 Mac Mini上使用
sysctl -n machdep.cpu.brand_string
输出Apple M1
,但是在Raspberry Pi 4 Ubuntu服务器上输出以下内容: