检测"Windows上的Ubuntu"与来自bash脚本的本机Ubuntu [英] Detect "Ubuntu on Windows" vs native Ubuntu from bash script
问题描述
bash脚本能否检测它是否在"Windows上的Ubuntu"与本机Ubuntu中运行?如果可以,怎么办?
Can a bash script detect if it's running in "Ubuntu on Windows" vs native Ubuntu? If so, how?
我在两台计算机上都运行了env
,但没有看到任何明显的环境变量差异.我可以测试/mnt/c
目录是否存在,但这并不是万无一失的,因为该目录也可能存在于本机Ubuntu中.
I ran env
on both machines and didn't see any obvious environmental variable differences. I could test for the existence of the /mnt/c
directory, but that is not foolproof because that directory could potentially also be present on native Ubuntu.
推荐答案
在Windows上的Ubuntu中,/proc/version
看起来包含:
It looks like /proc/version
in Ubuntu on Windows contains:
Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014
我的Ubuntu版本具有:
and my version of Ubuntu has:
Linux version 4.4.0-31-generic (buildd@lgw01-16) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1) ) #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016
此代码对我有用,可以检测脚本在哪个版本的Ubuntu上运行:
This code is working for me to detect which version of Ubuntu the script is running on:
if grep -q Microsoft /proc/version; then
echo "Ubuntu on Windows"
else
echo "native Linux"
fi
这篇关于检测"Windows上的Ubuntu"与来自bash脚本的本机Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!