怎么用脚本获取操作系统架构

wen 实用脚本 2

本文目录导读:

怎么用脚本获取操作系统架构

  1. Bash/Linux Shell
  2. Python
  3. Node.js/JavaScript
  4. PowerShell (Windows)
  5. Ruby
  6. Java
  7. 跨平台脚本 (Bash + Windows)
  8. 简单脚本(单行命令)
  9. 注意事项

Bash/Linux Shell

#!/bin/bash
# 获取操作系统架构
# 方法1:使用 uname
arch=$(uname -m)
echo "架构: $arch"
# 方法2:使用 arch 命令
arch2=$(arch)
echo "架构: $arch2"
# 方法3:使用 dpkg(Debian/Ubuntu)
if command -v dpkg &> /dev/null; then
    arch3=$(dpkg --print-architecture)
    echo "架构: $arch3"
fi
# 方法4:检查系统文件
arch4=$(cat /etc/redhat-release 2>/dev/null || cat /etc/os-release)

Python

#!/usr/bin/env python3
import platform
import os
import sys
# 方法1:使用 platform 模块
print(f"系统架构: {platform.machine()}")
print(f"处理器: {platform.processor()}")
print(f"系统: {platform.system()} {platform.release()}")
# 方法2:使用 os 模块
print(f"OS 架构: {os.uname().machine}")
# 方法3:检查是否为 64 位
print(f"64位系统: {sys.maxsize > 2**32}")
# 方法4:获取 Python 自身的架构
print(f"Python 位数: {platform.architecture()}")

Node.js/JavaScript

// Node.js
const os = require('os');
const { exec } = require('child_process');
// 方法1:使用 os 模块
console.log(`CPU 架构: ${os.arch()}`);
console.log(`操作系统: ${os.platform()}`);
console.log(`系统版本: ${os.release()}`);
console.log(`CPU 信息:`, os.cpus());
// 方法2:使用 exec
exec('uname -m', (error, stdout, stderr) => {
    if (!error) {
        console.log(`系统架构: ${stdout.trim()}`);
    }
});
// 方法3:检查是否 64 位
console.log(`是否为 64 位: ${process.arch === 'x64'}`);

PowerShell (Windows)

# 方法1:使用环境变量
$arch = $env:PROCESSOR_ARCHITECTURE
Write-Host "处理器架构: $arch"
# 方法2:使用 WMI
$os = Get-WmiObject -Class Win32_OperatingSystem
Write-Host "系统架构: $($os.OSArchitecture)"
# 方法3:使用 SystemInfo
systeminfo | Select-String "System Type"
# 方法4:使用 .NET
[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture

Ruby

#!/usr/bin/env ruby
require 'etc'
require 'rbconfig'
# 方法1:使用 RUBY_PLATFORM
puts "Ruby 平台: #{RUBY_PLATFORM}"
# 方法2:使用 RbConfig
puts "架构: #{RbConfig::CONFIG['host_cpu']}"
# 方法3:使用 Etc
puts "处理器: #{Etc.nprocessors} 核"

Java

public class SystemArch {
    public static void main(String[] args) {
        // 方法1:获取系统属性
        String arch = System.getProperty("os.arch");
        System.out.println("系统架构: " + arch);
        // 方法2:获取系统信息
        String osName = System.getProperty("os.name");
        String osVersion = System.getProperty("os.version");
        System.out.println("操作系统: " + osName + " " + osVersion);
        // 方法3:使用 Java 9+
        // System.out.println("OS: " + System.getProperty("os.name"));
    }
}

跨平台脚本 (Bash + Windows)

#!/bin/bash
# 跨平台检测
detect_arch() {
    case "$OSTYPE" in
        linux*|darwin*)
            arch=$(uname -m)
            ;;
        msys*|cygwin*|win32*)
            arch=$PROCESSOR_ARCHITECTURE
            ;;
        *)
            arch="unknown"
            ;;
    esac
    case "$arch" in
        x86_64|amd64)
            echo "64-bit (x86_64)"
            ;;
        i386|i686|x86)
            echo "32-bit (x86)"
            ;;
        arm64|aarch64)
            echo "ARM 64-bit"
            ;;
        arm*)
            echo "ARM 32-bit"
            ;;
        *)
            echo "Unknown architecture: $arch"
            ;;
    esac
}
detect_arch

简单脚本(单行命令)

# Linux/Unix
uname -m  # 输出如:x86_64, arm64 等
# Windows CMD
echo %PROCESSOR_ARCHITECTURE%
# Windows PowerShell
[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture

注意事项

  • 不同系统返回值可能不同:如 x86_64amd64x64 表示相同的 64 位 x86 架构
  • ARM 架构arm64aarch64armv8 等表示 ARM 64 位
  • 嵌入式系统:可能有特殊的架构名称
  • 交叉编译环境:检测结果可能不是实际运行环境

选择哪种方法取决于你的使用场景和脚本语言偏好,在 Linux 环境中,最常用的是 uname -m;在 Windows 中,推荐使用 PowerShell 或环境变量。

抱歉,评论功能暂时关闭!