如何获取描述当前平台的字符串?
我需要检查我所在的平台,即:处理器、操作系统和操作系统版本。我目前拥有的一个示例是i686-apple-darwin10
。如何在Linux和Windows上获取相似的字符串?我知道自动工具提供了一个生成该字符串的检测脚本,但我现在不记得它的名称,并且由于它在 bash 中,因此它无法在 Windows 上运行。
一个相关的问题是:我从未真正了解检测平台的技术。这有标准吗?例如,“i686-apple-darwin10”就是其中之一,而且我还看到了 uname
-uname -p
样式:“Linux-i386”。每个平台字符串的最佳实践、标准字符串和首选上下文是什么?
I need to check the platform I'm on, meaning: processor, operating system, and operating system version. An example I currently have is i686-apple-darwin10
. How can I obtain the similar string on Linux and Windows? I know that the autotools provide a detection script that produces that string, but I don't recall its name now, and since it's in bash, it won't work on windows.
A related question is: I've never really got to understand the techniques to detect a platform. Are there standards for this ? For example, "i686-apple-darwin10" is one, but also I saw the uname
-uname -p
style: "Linux-i386". What are the best-practices, standard strings and preferred context for each platform string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 GNU/Linux 系统上,您可能需要
uname
和lsb_release
的组合。下面是您可以使用
uname
获得的内容:现在使用
lsb_release
:On a GNU/Linux system, you might need a combination of
uname
andlsb_release
.Below what you can get with
uname
:And now with
lsb_release
:作为起点,您需要指定您想要进行此检测的语言。
如果您指定“我想要一个可以在任何计算机上运行并识别它的程序”,那么就忘记它 - 这是行不通的。任何程序文件都不能在任何计算机上运行——更不用说识别它了。一种选择是“使用系统上已知的程序”,但没有一个这样的程序是普遍可用的。
沃特。编程语言,您可以在“脚本”和“编译”之间进行选择(也许中间有“字节码编译”)。在脚本语言中,您需要选择一种解释器已在系统上的语言,或者您可以接受用户在运行脚本之前先安装解释器的语言。这就是 Unix shell 脚本如此广泛用于系统识别的原因。当然,它们仅限于提供
/bin/sh
的系统。对于编译语言,您可以选择编译时系统识别和运行时系统识别。编译时间在 C 语言中确实很常见,并且各种编译器都有预定义的宏,您可以使用它们来识别编译器、操作系统或微处理器。
对于运行时,您需要具有可以使用的系统调用/库函数。您可以使用哪些取决于编程语言。
As the starting point, you need to specify what language you want this detection in.
If you specify "I want a single program that can run on any computer and identify it", then forget it - this won't work. No program file can run on any computer - let alone identifying it. One option would be "use a program that is already known to be on the system", but no single such program is universally available.
Wrt. programming languages, you have the choice between "scripting" and "compiled" (perhaps with "byte-code compiled" in-between). In scripting languages, you need to chose one where the interpreter is already on the system, or where you can accept that the user installs the interpreter first before running your script. That's why Unix shell scripts are so widely used for system identification. They are, of course, restricted to systems that provide
/bin/sh
.For compiled languages, you have the choice between compile-time and run-time system identification. Compile time is really common in C, and various compilers have predefined macros that you can use to identify the compiler, or the operating system, or the microprocessor.
For run-time, you need to have system calls/library functions that you can use. Which ones you can use depends on the programming language.
没有标准的跨平台方法可以做到这一点,也没有“标准”字符串。
Linux/UNIX 上的 uname 会为您提供一些信息,但通常仅有关正在运行的内核,而不是有关平台的信息(当您谈论 Linux 时,“操作系统版本”是什么意思?内核版本?操作系统版本,例如 RHEL 5.3?)。在 Windows 中,您可以询问 WMI,但没有命令行为您提供信息。
There is no standard, cross-platform way to do this, and there is no "standard" string.
uname on Linux/UNIX gets you some information, but generally only about the running kernel and not about the platform (when you're talking Linux, what do you mean by "operating system version"? Kernel version? OS version e.g. RHEL 5.3?). Windows you can interrogate WMI but there's no commandline to give you the info.
如果系统上存在
python
(它可用于所有主要平台),则:输出:
Linux-2.6.28-15-generic-i686-with-Ubuntu-9.04-jaunty
代码>If
python
present on the system (it is available for all major platforms) then:Output:
Linux-2.6.28-15-generic-i686-with-Ubuntu-9.04-jaunty
我只能为 Linux 和 Windows 提供帮助。我不知道其他操作系统,但可能 *nixes 与 Linux 相同。
版本:Windows:
ver
,Linux:uname -a
。CPU:在 Linux 中使用 /proc/cpuinfo 文件,Windows 检查注册表(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ACPI\ 搜索 Class=Processor 的那个)
I can only help for Linux and Windows. I dont know for other o/s, but probably *nixes is same as Linux.
Version: Windows:
ver
, Linux:uname -a
.CPU: In Linux use /proc/cpuinfo file, Windows check registery (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ACPI\ search for the one with Class=Processor)
在静默模式下运行 dxdiag 将为您提供 Windows 上的各种信息。
等待 .xml 文件存在。各种信息。
Running dxdiag in silent mode will give you all kinds of information on windows.
Wait for existance of .xml file. All kinds of information.