怎么用脚本获取系统名称

wen 实用脚本 1

本文目录导读:

怎么用脚本获取系统名称

  1. Python(跨平台)
  2. Shell脚本(Linux/macOS)
  3. Batch脚本(Windows)
  4. PowerShell(Windows)
  5. Node.js(跨平台)
  6. Go语言(跨平台)
  7. 常用系统返回值参考

获取系统名称的方法取决于你使用的操作系统和脚本语言,以下是几种常见的方法:

Python(跨平台)

import platform
# 获取系统名称
system_name = platform.system()
print(system_name)  # 输出: 'Linux', 'Windows', 'Darwin'(macOS)
# 获取更详细的信息
print(platform.uname())

Shell脚本(Linux/macOS)

#!/bin/bash
# 方式1: 使用uname命令
echo "System: $(uname -s)"
# 方式2: 查看/etc/os-release
source /etc/os-release
echo "Distribution: $NAME"
# 方式3: 使用hostnamectl
hostnamectl | grep "Operating System"

Batch脚本(Windows)

@echo off
rem 方式1: 使用ver命令
ver
rem 方式2: 使用wmic
wmic os get Caption
rem 方式3: 使用systeminfo
systeminfo | findstr /B /C:"OS Name"

PowerShell(Windows)

# 方式1
(Get-WmiObject Win32_OperatingSystem).Caption
# 方式2
[System.Environment]::OSVersion
# 方式3
Get-ComputerInfo -Property "OsName"

Node.js(跨平台)

const os = require('os');
// 获取系统类型
console.log(os.type());    // 'Linux', 'Windows_NT', 'Darwin'
console.log(os.platform()); // 'linux', 'win32', 'darwin'

Go语言(跨平台)

package main
import (
    "fmt"
    "runtime"
)
func main() {
    fmt.Println(runtime.GOOS) // 输出: linux, windows, darwin
}

常用系统返回值参考

系统 Python platform.system() Shell uname -s
Linux 'Linux' 'Linux'
Windows 'Windows' 'MINGW64_NT-...' (Git Bash)
macOS 'Darwin' 'Darwin'

选择适合你开发环境的方法即可,如果需要更详细的系统信息,可以结合其他命令或函数使用。

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