如何从 Bash 脚本中检测操作系统? [英] How to detect the OS from a Bash script?

查看:23
本文介绍了如何从 Bash 脚本中检测操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的 .bashrc.bash_login 文件保留在版本控制中,以便我可以在我使用的所有计算机之间使用它们.问题是我有一些特定于操作系统的别名,所以我正在寻找一种方法来确定脚本是在 Mac OS X、Linux 还是 Cygwin.

I would like to keep my .bashrc and .bash_login files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running on Mac OS X, Linux or Cygwin.

Bash 脚本中检测操作系统的正确方法是什么?

What is the proper way to detect the operating system in a Bash script?

推荐答案

我认为以下应该可行.不过我不确定 win32.

I think the following should work. I'm not sure about win32 though.

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # ...
elif [[ "$OSTYPE" == "darwin"* ]]; then
        # Mac OSX
elif [[ "$OSTYPE" == "cygwin" ]]; then
        # POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
        # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
        # I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
        # ...
else
        # Unknown.
fi

这篇关于如何从 Bash 脚本中检测操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆