Answers:
通常,uname它具有各种选项,它们会告诉您您正在哪个环境中运行:
pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
pax> uname -s
CYGWIN_NT-5.1
并且,根据非常有帮助的schot(在注释中),uname -s给出Darwin了OSX和LinuxLinux的代码,而我的Cygwin给出了CYGWIN_NT-5.1。但是您可能必须尝试各种不同的版本。
因此bash,执行此检查的代码如下:
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo ${machine}
请注意,我在这里假设您实际上是在 CygWin(bash它的外壳)中运行,因此路径应该已经正确设置。正如一位评论者所述,您可以bash从cmd自己传递脚本的方式运行该程序,这可能会导致未根据需要设置路径。
如果你正在这样做,这是你的责任,以确保正确的可执行文件(即,在Cygwin的)都被调用,可能通过修改路径提前或完全指定可执行文件的位置(例如,/c/cygwin/bin/uname)。
MINGW32_NT-6.1。此外,没有/cygdrive前缀,仅/c用于C:。
How can a shell/bash script detect ...,另一个涉及。
uname -s最终调用uname当前路径中的第一个路径,而在我的系统上,该路径原来是安装的版本,可用geda来返回text WindowsNT。不过,它也可以是如上所述的MinGW版本。对cygwin的可靠检测不得依赖于正确设置的路径IMO。因此,$(uname -s)应更改$(/bin/uname -s)为检测cygwin。
#!/usr/bin/env bash而不是#!/bin/sh来防止由于/bin/sh链接到不同平台上的不同默认Shell所引起的问题,否则将出现类似意料之外的错误的错误,这就是我的计算机上发生的情况(Ubuntu 64位12.04)。expr除非您安装了Mac OS X 10.6.8(Snow Leopard),否则没有程序,因此我只使用uname。uname来获取系统信息(-s参数)。expr和substr处理字符串。if elif fi做匹配工作。uname -s。#!/usr/bin/env bash
if [ "$(uname)" == "Darwin" ]; then
# Do something under Mac OS X platform
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
# Do something under GNU/Linux platform
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
# Do something under 32 bits Windows NT platform
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
# Do something under 64 bits Windows NT platform
fi
[ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]。
"$(expr substr $(uname -s) 1 5)"。有更多漂亮的方法可以做到这一点,例如:if [ `uname -s` == CYGWIN* ]; then。阅读它:如果uname -s以CYGWIN开始,那么...
if [[ $(uname -s) == CYGWIN* ]]; then
uname -s将产生除“ Linux”以外的其他内容?
使用uname -s(--kernel-name)是因为在某些操作系统(例如Mac OS和Solaris)上不支持uname -o(--operating-system)。您也可以不带任何参数使用,因为默认参数为()。uname-s--kernel-name
以下代码段不需要 重击(即不需要#!/bin/bash)
#!/bin/sh
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
;;
Linux)
echo 'Linux'
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*)
echo 'MS Windows'
;;
# Add here more strings to compare
# See correspondence table at the bottom of this answer
*)
echo 'Other OS'
;;
esac
以下Makefile内容受Git项目(config.mak.uname)的启发。
ifdef MSVC # Avoid the MingW/Cygwin sections
uname_S := Windows
else # If uname not available => 'not'
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
endif
# Avoid nesting "if .. else if .. else .. endif endif"
# because maintenance of matching if/else/endif is a pain
ifeq ($(uname_S),Windows)
CC := cl
endif
ifeq ($(uname_S),OSF1)
CFLAGS += -D_OSF_SOURCE
endif
ifeq ($(uname_S),Linux)
CFLAGS += -DNDEBUG
endif
ifeq ($(uname_S),GNU/kFreeBSD)
CFLAGS += -D_BSD_ALLOC
endif
ifeq ($(uname_S),UnixWare)
CFLAGS += -Wextra
endif
...
该答案底部的对应表来自Wikipedia文章uname。请做出贡献以使其保持最新(编辑答案或发表评论)。您也可以更新Wikipedia文章并发表评论以通知我有关您的贡献;-)
Operating System uname -s
Mac OS X Darwin
Cygwin 32-bit (Win-XP) CYGWIN_NT-5.1
Cygwin 32-bit (Win-7 32-bit)CYGWIN_NT-6.1
Cygwin 32-bit (Win-7 64-bit)CYGWIN_NT-6.1-WOW64
Cygwin 64-bit (Win-7 64-bit)CYGWIN_NT-6.1
MinGW (Windows 7 32-bit) MINGW32_NT-6.1
MinGW (Windows 10 64-bit) MINGW64_NT-10.0
Interix (Services for UNIX) Interix
MSYS MSYS_NT-6.1
MSYS2 MSYS_NT-10.0-17763
Windows Subsystem for Linux Linux
Android Linux
coreutils Linux
CentOS Linux
Fedora Linux
Gentoo Linux
Red Hat Linux Linux
Linux Mint Linux
openSUSE Linux
Ubuntu Linux
Unity Linux Linux
Manjaro Linux Linux
OpenWRT r40420 Linux
Debian (Linux) Linux
Debian (GNU Hurd) GNU
Debian (kFreeBSD) GNU/kFreeBSD
FreeBSD FreeBSD
NetBSD NetBSD
DragonFlyBSD DragonFly
Haiku Haiku
NonStop NONSTOP_KERNEL
QNX QNX
ReliantUNIX ReliantUNIX-Y
SINIX SINIX-Y
Tru64 OSF1
Ultrix ULTRIX
IRIX 32 bits IRIX
IRIX 64 bits IRIX64
MINIX Minix
Solaris SunOS
UWIN (64-bit Windows 7) UWIN-W7
SYS$UNIX:SH on OpenVMS IS/WB
z/OS USS OS/390
Cray sn5176
(SCO) OpenServer SCO_SV
(SCO) System V SCO_SV
(SCO) UnixWare UnixWare
IBM AIX AIX
IBM i with QSH OS400
HP-UX HP-UX
~/.profile(设置环境变量,例如$PATH-注释以提供后代搜索关键字)所需的内容。
uname -sr和比较反对Linux*Microsoft)前Linux*)。
Bash设置外壳程序变量OSTYPE。来自man bash:
自动设置为描述执行bash的操作系统的字符串。
uname与之相比,它有一个微小的优势,因为它不需要启动新进程,因此执行起来会更快。
但是,我找不到权威的期望值列表。对于我来说,在Ubuntu 14.04上,它设置为'linux-gnu'。我在网上刮了一些其他价值。因此:
case "$OSTYPE" in
linux*) echo "Linux / WSL" ;;
darwin*) echo "Mac OS" ;;
win*) echo "Windows" ;;
msys*) echo "MSYS / MinGW / Git Bash" ;;
cygwin*) echo "Cygwin" ;;
bsd*) echo "BSD" ;;
solaris*) echo "Solaris" ;;
*) echo "unknown: $OSTYPE" ;;
esac
星号在某些情况下很重要-例如OSX在'darwin'之后附加OS版本号。有人告诉我'win'值实际上是'win32'-也许有一个'win64'吗?
也许我们可以一起在此处填充经过验证的值的表格:
linux-gnucygwinmsys(如果与现有条目不同,请附加您的值)
env | grep OSTYPE,但是您会在下面看到它set | grep OSTYPE
OSTYPE变量(conftypes.h)在构建时使用automake OS变量的精确副本(Makefile.in)进行配置。可以参考automake的lib / config.sub文件获取所有可用类型。
为了建立在阿尔伯特的答案上,我喜欢用它$COMSPEC来检测Windows:
#!/bin/bash
if [ "$(uname)" == "Darwin" ]
then
echo Do something under Mac OS X platform
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]
then
echo Do something under Linux platform
elif [ -n "$COMSPEC" -a -x "$COMSPEC" ]
then
echo $0: this script does not support Windows \:\(
fi
这样可以避免解析Windows名称用于的$OS变体,以及解析uname诸如MINGW,Cygwin等的变体。
背景:%COMSPEC%是Windows环境变量,用于指定命令处理器(也称为Windows Shell)的完整路径。该变量的值通常为%SystemRoot%\system32\cmd.exe,其值通常为C:\Windows\system32\cmd.exe。
# This script fragment emits Cygwin rulez under bash/cygwin
if [[ $(uname -s) == CYGWIN* ]];then
echo Cygwin rulez
else
echo Unix is king
fi
如果uname -s命令的前6个字符为“ CYGWIN”,则假定为cygwin系统
if [ `uname -s` == CYGWIN* ]; then看起来更好,工作原理相同。
[[ $(uname -s) == CYGWIN* ]]。还需要注意的是扩展的正则表达式是在我们的情况更精确:[[ $(uname -s) =~ ^CYGWIN* ]]。
expr substr $(uname -s) 1 6显示错误(expr: syntax error)。
好的,这是我的方式。
osis()
{
local n=0
if [[ "$1" = "-n" ]]; then n=1;shift; fi
# echo $OS|grep $1 -i >/dev/null
uname -s |grep -i "$1" >/dev/null
return $(( $n ^ $? ))
}
例如
osis Darwin &&
{
log_debug Detect mac osx
}
osis Linux &&
{
log_debug Detect linux
}
osis -n Cygwin &&
{
log_debug Not Cygwin
}
我在我的点文件中使用它
http://en.wikipedia.org/wiki/Uname
您将需要的所有信息。Google是您的朋友。
使用uname -s查询系统名称。
DarwinCYGWIN_...LINUX适用于大多数我想这个一致的答案是无与伦比的,主要是在清洁方面。
尽管执行起来花了很多时间,但我发现测试特定文件的存在也会使我获得更好更快的结果,因为我没有调用可执行文件:
所以,
[ -f /usr/bin/cygwin1.dll ] && echo Yep, Cygwin running
只是使用快速的Bash文件存在性检查。就像我现在在Windows上一样,我无法从脑海中告诉您Linux和Mac OS X的任何特定文件,但是我很确定它们确实存在。:-)
多亏了贾斯汀,在命令行中仅使用此命令就可以很好地工作:
#!/bin/bash
################################################## #########
# Bash script to find which OS
################################################## #########
OS=`uname`
echo "$OS"