Detect CPU Architecture in Shell Scripts

Detect CPU Architecture in Shell Scripts

6月 11, 2024 · 1 分钟阅读时长 · 96 字 · -阅读 -评论

While writing an installer for Code Server, I needed to identify the machine’s CPU architecture to fetch the right tarball.

Code Server Packages

Different architectures require different builds:

https://static.1991421.cn/2024/2024-06-11-183158.jpeg

Script

uname reports the architecture:

arch() {
  uname_m=$(uname -m)
  case $uname_m in
    aarch64) echo arm64 ;;
    x86_64) echo amd64 ;;
    *) echo "$uname_m" ;;
  esac
}

ARCH=${ARCH:-$(arch)}

Once you know the architecture, downloading the matching package is trivial.

Cloud Server Architectures

Cloud providers (e.g., Tencent Cloud) display CPU architecture when you provision instances, so you can choose what you need up front:

https://static.1991421.cn/2024/2024-06-11-184222.jpeg

Final Notes

That’s it—simple and effective.

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源