如何检查是否在 Cygwin、Mac 或 Linux 中运行? [英] How to check if running in Cygwin, Mac or Linux?

查看:26
本文介绍了如何检查是否在 Cygwin、Mac 或 Linux 中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 shell 脚本,可在 Windows/Cygwin、Mac 和 Linux 上使用.每个版本需要稍微不同的变量.

I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.

shell/bash 脚本如何检测它是在 Cygwin、Mac 还是 Linux 中运行?

How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux?

推荐答案

通常,uname 及其各种选项会告诉你你在什么环境中运行:

Usually, uname with its various options will tell you what environment you're running in:

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 为 OSX 和 Linux 提供 Darwin 适用于 Linux,而我的 Cygwin 提供 CYGWIN_NT-5.1.但是您可能需要尝试各种不同的版本.

And, according to the very helpful schot (in the comments), uname -s gives Darwin for OSX and Linux for Linux, while my Cygwin gives CYGWIN_NT-5.1. But you may have to experiment with all sorts of different versions.

因此,执行此类检查的 bash 代码将遵循以下内容:

So the bash code to do such a check would be along the lines of:

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 shell),因此应该已经正确设置了路径.正如一位评论者所指出的,您可以运行 bash 程序,并从 cmd 本身传递脚本,这可能会导致未根据需要设置路径.


Note that I'm assuming here that you're actually running within CygWin (the bash shell of it) so paths should already be correctly set up. As one commenter notes, you can run the bash program, passing the script, from cmd itself and this may result in the paths not being set up as needed.

如果您正在这样做,您有责任确保调用正确的可执行文件(即 CygWin 的),可能是通过事先修改路径或完全指定可执行文件的位置(例如,/c/cygwin/bin/uname).

If you are doing that, it's your responsibility to ensure the correct executables (i.e., the CygWin ones) are being called, possibly by modifying the path beforehand or fully specifying the executable locations (e.g., /c/cygwin/bin/uname).

这篇关于如何检查是否在 Cygwin、Mac 或 Linux 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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