本文翻译自:How to check if running in Cygwin, Mac or Linux?

I have a shell script that is used both on Windows/Cygwin and Mac and Linux. 我有一个shell脚本,可以在Windows / Cygwin,Mac和Linux上使用。 It needs slightly different variables for each versions. 每个版本需要稍微不同的变量。

How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux? shell / bash脚本如何检测它是在Cygwin,Mac或Linux上运行?


#1楼

参考:https://stackoom.com/question/exHU/如何检查是否在Cygwin-Mac或Linux中运行


#2楼

# This script fragment emits Cygwin rulez under bash/cygwin
if [[ $(uname -s) == CYGWIN* ]];thenecho Cygwin rulez
else echo Unix is king
fi

If the 6 first chars of uname -s command is "CYGWIN", a cygwin system is assumed 如果uname -s命令的6个第一个字符是“CYGWIN”,则假定为cygwin系统


#3楼

Here is the bash script I used to detect three different OS type (GNU/Linux, Mac OS X, Windows NT) 这是我用来检测三种不同操作系统类型的bash脚本(GNU / Linux,Mac OS X,Windows NT)

Pay attention 请注意

  • In your bash script, use #!/usr/bin/env bash instead of #!/bin/sh to prevent the problem caused by /bin/sh linked to different default shell in different platforms, or there will be error like unexpected operator , that's what happened on my computer (Ubuntu 64 bits 12.04). 在你的bash脚本,使用#!/usr/bin/env bash ,而不是#!/bin/sh ,以防止因问题/bin/sh意外操作连接到不同的默认外壳在不同的平台上,否则会有错误,这就是我的电脑上发生的事情(Ubuntu 64 bit 12.04)。
  • Mac OS X 10.6.8 (Snow Leopard) do not have expr program unless you install it, so I just use uname . 除非你安装了Mac OS X 10.6.8(Snow Leopard)没有expr程序,所以我只使用uname

Design 设计

  1. Use uname to get the system information ( -s parameter). 使用uname获取系统信息( -s参数)。
  2. Use expr and substr to deal with the string. 使用exprsubstr来处理字符串。
  3. Use if elif fi to do the matching job. 使用if elif fi来完成匹配的工作。
  4. You can add more system support if you want, just follow the uname -s specification. 如果需要,可以添加更多系统支持,只需遵循uname -s规范即可。

Implementation 履行

#!/usr/bin/env bashif [ "$(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

Testing 测试

  • Linux (Ubuntu 12.04 LTS, Kernel 3.2.0) tested OK. Linux(Ubuntu 12.04 LTS,内核3.2.0)测试好了。
  • OS X (10.6.8 Snow Leopard) tested OK. OS X(10.6.8 Snow Leopard)测试好了。
  • Windows (Windows 7 64 bit) tested OK. Windows(Windows 7 64位)测试正常。

What I learned 我学到的是

  1. Check for both opening and closing quotes. 检查开盘价和收盘价。
  2. Check for missing parentheses and braces {} 检查是否缺少括号和括号{}

References 参考

  • [1] uname - wikipedia [1] uname - 维基百科
  • [2] shell script syntax error: unexpected end of file [2] shell脚本语法错误:意外的文件结束
  • [3] Detect the OS from a Bash script [3] 从Bash脚本中检测操作系统
  • [4] BASH Programming Introduction HOW-TO [4] BASH编程介绍如何操作

#4楼

To build upon Albert's answer, I like to use $COMSPEC for detecting Windows: 为了建立Albert的答案,我喜欢用$COMSPEC来检测Windows:

#!/bin/bashif [ "$(uname)" == "Darwin" ]
thenecho Do something under Mac OS X platform
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]
thenecho Do something under Linux platform
elif [ -n "$COMSPEC" -a -x "$COMSPEC" ]
then echo $0: this script does not support Windows \:\(
fi

This avoids parsing variants of Windows names for $OS , and parsing variants of uname like MINGW, Cygwin, etc. 这样可以避免为$OS解析Windows名称的变体,并解析像MINGW,Cygwin等的uname变体。

Background: %COMSPEC% is a Windows environmental variable specifying the full path to the command processor (aka the Windows shell). 背景: %COMSPEC%是Windows环境变量,指定命令处理器(也称为Windows shell)的完整路径。 The value of this variable is typically %SystemRoot%\\system32\\cmd.exe , which typically evaluates to C:\\Windows\\system32\\cmd.exe . 此变量的值通常为%SystemRoot%\\system32\\cmd.exe ,通常计算结果为C:\\Windows\\system32\\cmd.exe


#5楼

Use only this from command line works very fine, thanks to Justin: 感谢Justin,只使用命令行中的这个工作非常好

#!/bin/bash################################################## #########
# Bash script to find which OS
################################################## #########OS=`uname`
echo "$OS"

source 资源


#6楼

Use uname -s ( --kernel-name ) because uname -o ( --operating-system ) is not supported on some Operating Systems as Mac OS , Solaris . 使用uname -s ( - --kernel-name ),因为某些操作系统不支持uname -o ( - --operating-system )作为Mac OSSolarisYou may also use just uname without argument as the default argument is -s ( --kernel-name ). 您也可以只使用uname而不使用参数,因为默认参数是-s ( - --kernel-name )。

The below snippet does not require bash (ie does not require #!/bin/bash ) 下面的代码片段不需要bash (即不需要#!/bin/bash

#!/bin/shcase "$(uname -s)" inDarwin)echo 'Mac OS X';;Linux)echo 'Linux';;CYGWIN*|MINGW32*|MSYS*)echo 'MS Windows';;# Add here more strings to compare# See correspondence table at the bottom of this answer*)echo 'other OS' ;;
esac

The below Makefile is inspired from Git project ( config.mak.uname ) . 下面的Makefile灵感来自Git项目( config.mak.uname ) 。

ifdef MSVC     # Avoid the MingW/Cygwin sectionsuname_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 painifeq ($(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
...

See also this complete answer about uname -s and Makefile . 另请参阅有关uname -sMakefile完整答案 。

The correspondence table in the bottom of this answer is from Wikipedia article about uname . 这个答案底部的对应表来自维基百科关于uname文章 。 Please contribute to keep it up-to-date (edit the answer or post a comment). 请提供帮助以使其保持最新(编辑答案或发表评论)。 You may also update the Wikipedia article and post a comment to notice me about your contribution ;-) 您还可以更新维基百科文章并发表评论以通知我您的贡献;-)

Operating System uname -s Operating System uname -s
Mac OS X Darwin Mac OS X Darwin
Cygwin 32-bit (Win-XP) CYGWIN_NT-5.1 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 32-bit) CYGWIN_NT-6.1
Cygwin 32-bit (Win-7 64-bit) CYGWIN_NT-6.1-WOW64 Cygwin 32-bit (Win-7 64-bit) CYGWIN_NT-6.1-WOW64
Cygwin 64-bit (Win-7 64-bit) CYGWIN_NT-6.1 Cygwin 64-bit (Win-7 64-bit) CYGWIN_NT-6.1
MinGW (Windows 7 32-bit) MINGW32_NT-6.1 MinGW (Windows 7 32-bit) MINGW32_NT-6.1
MinGW (Windows 10 64-bit) MINGW64_NT-10.0 MinGW (Windows 10 64-bit) MINGW64_NT-10.0
Interix (Services for UNIX) Interix MSYS MSYS_NT-6.1 Windows Subsystem for Linux Linux Interix (Services for UNIX) Interix MSYS MSYS_NT-6.1 Windows Subsystem for Linux Linux Windows Subsystem for Linux
Android Linux Android Linux
coreutils Linux coreutils Linux
CentOS Linux CentOS Linux
Fedora Linux Fedora Linux
Gentoo Linux Gentoo Linux
Red Hat Linux Linux Red Hat Linux Linux
Linux Mint Linux Linux Mint Linux
openSUSE Linux openSUSE Linux
Ubuntu Linux Ubuntu Linux
Unity Linux Linux Unity Linux Linux
Manjaro Linux Linux Manjaro Linux Linux
OpenWRT r40420 Linux OpenWRT r40420 Linux
Debian (Linux) Linux Debian (Linux) Linux
Debian (GNU Hurd) GNU Debian (GNU Hurd) GNU
Debian (kFreeBSD) GNU/kFreeBSD Debian (kFreeBSD) GNU/kFreeBSD
FreeBSD FreeBSD FreeBSD FreeBSD
NetBSD NetBSD NetBSD NetBSD
DragonFlyBSD DragonFly DragonFlyBSD DragonFly
Haiku Haiku Haiku Haiku
NonStop NONSTOP_KERNEL NonStop NONSTOP_KERNEL
QNX QNX QNX QNX
ReliantUNIX ReliantUNIX-Y ReliantUNIX ReliantUNIX-Y
SINIX SINIX-Y SINIX SINIX-Y
Tru64 OSF1 Tru64 OSF1
Ultrix ULTRIX Ultrix ULTRIX
IRIX 32 bits IRIX IRIX 32 bits IRIX
IRIX 64 bits IRIX64 IRIX 64 bits IRIX64
MINIX Minix MINIX Minix
Solaris SunOS Solaris SunOS
UWIN (64-bit Windows 7) UWIN-W7 UWIN (64-bit Windows 7) UWIN-W7
SYS$UNIX:SH on OpenVMS IS/WB SYS$UNIX:SH on OpenVMS IS/WB SYS$UNIX:SH on OpenVMS
z/OS USS OS/390 z/OS USS OS/390
Cray sn5176 Cray sn5176
(SCO) OpenServer SCO_SV (SCO) OpenServer SCO_SV
(SCO) System V SCO_SV (SCO) System V SCO_SV
(SCO) UnixWare UnixWare (SCO) UnixWare UnixWare
IBM AIX AIX IBM AIX AIX
IBM i with QSH OS400 IBM i with QSH OS400
HP-UX HP-UX HP-UX HP-UX

如何检查是否在Cygwin,Mac或Linux中运行?相关推荐

  1. 在Windows,Mac,Linux中快速安装配置Node.js环境,并安装VSCode, 完成Web端恒生交易日接口的图形化展示...

    编程课应该怎么制作? 编程的乐趣应来自实用主义,我大学本科第一门Java编程课,几乎劝退了所有同学,因为那些教学代码不实用且无趣,一点图形化的内容都没有,而实用的编程课应早早展现图形化的成果, 于是我 ...

  2. linux以非root身份运行,以非root用户身份在linux中运行mono-service

    我需要在嵌入式系统上以最低Ubuntu安装方式运行.net C#应用程序(在Windows系统上开发)作为服务/守护程序(不包括X,除服务器外SSH,只有相关的软件).我创建了一个/etc/init. ...

  3. eclipse中linux打包,Eclipse中Maven打包程序并在Linux中运行

    Eclipse中Maven打包程序并在Linux中运行 1 在Eclipse中新建Maven工程 新建后的maven工程如下: 新建Maven工程的默认pom.xml如下,不需要修改: 4.0.0 T ...

  4. linux中运行ifconfig出现错误,不能sudo apt install net-tools,Linux不能联网

    linux中运行ifconfig出现错误,且不能sudo apt install net-tools linux中运行ifconfig出现错误 不能sudo apt install net-tools ...

  5. Windows下的脚本在Linux中运行乱码问题

    Windows下的py脚本在Linux中运行 dos下写的代码,拿到linux下,存在不兼容. 解决办法: 方法一:dos2unix 如果没有该插件 需要安装 sudo apt-get install ...

  6. 在Linux中运行Android软件

    想要在PC上运行Android软件就需要模拟器,Windows版的Android模拟器软件很多,但是Linux平台的就不多了,今天推荐一款Linux下的Android模拟器软件Anbox Anbox官 ...

  7. 在Linux中运行Nancy应用程序

    最近在研究如何将.NET应用程序移植到非Windows操作系统中运行,逐渐会写一些文章出来.目前还没有太深的研究,所以这些文章大多主要是记录我的一些实验. 这篇文章记录了我如何利用NancyFx编写一 ...

  8. LINUX中运行java程序的方法

    要想在linux中运行java的项目需要先将项目打包成war包或者jar包. 其中打包成war包需要将war包部署到tomcat服务器上才能运行.而打包成jar包可以直接使用java命令执行. 在li ...

  9. Linux中运行可执行文件时找不到lib文件

    动态链接库( libjthread 也是工程内的一个子项目)找不着,怎么回事? make install 安装的时候,是把动态链接库和执行文件都放在同一个目录下的 在 CMP0042 更新,也就是Cm ...

最新文章

  1. CVPR 2020夜间行人检测挑战赛两冠一亚:DeepBlueAI团队获胜方案解读
  2. groovy 使用java类_在java中使用groovy怎么搞 (java and groovy)
  3. 如何修改xd.properties文件中对象存储文件信息_对块存储、文件存储、对象存储的认识总结...
  4. 阿里巴巴创新研究计划AIR2018正式发布 邀全球学者共创未来
  5. 那些一眼就被看出包装过的简历
  6. 五大软件设计原则学习笔记5——依赖倒置原则
  7. rtcp webrtc 接收_WebRTC RTP/RTCP 源码分析(四):RTCP 的接收和解析
  8. php里面get和post请求,php中GET和POST请求发送几种方法总结
  9. 【廖雪峰官方网站/Java教程】泛型
  10. chrome支持的java版本下载_安装Chrome Java插件
  11. 常见c语言语法错误,C语言常见语法错误.doc
  12. 我的家乡html网页设计,创作一个以“我的家乡”为主题的网站
  13. 【计组】超标量、超级流水线、超长指令字区别详解
  14. iTextSharp显示中文
  15. 用eviews建立sarima模型_计量经济学第10讲(时间序列计量经济学模型:序列相关性)...
  16. ubuntu开机桌面不显示
  17. Tilemap瓦片资源
  18. vue 找回密码_找回密码的功能设计
  19. 支付宝集福攻略,作为程序员的你集福了么?
  20. 佳能扫描仪按下按钮后自动打开phtoshop怎么办

热门文章

  1. 17级中等职业学校计算机水平,实习风采 | 17级计算机1班商齐:坚持 遇见更好的自己!...
  2. win10系统安装iso文件,绝对可下。
  3. c语言中如何让数字反过来,在C语言中,如何输出逆序的数字
  4. 弘辽科技:淘宝新品怎么打标?有哪些作用?
  5. 如何查看自己chrome的版本
  6. 中国十大高薪职业出炉
  7. 高精地图构建与SLAM感知优化建图策略
  8. 使用domtoimage插件生成图片模糊失真问题
  9. 微信小程序的常用组件
  10. 发票管理之发票识别技术的应用