Detect “Ubuntu on Windows” vs native Ubuntu from bash script [duplicate]

左心房为你撑大大i 提交于 2019-12-29 03:34:08

问题


Can a bash script detect if it's running in "Ubuntu on Windows" vs native Ubuntu? If so, how?

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.


回答1:


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

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

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


来源:https://stackoverflow.com/questions/38859145/detect-ubuntu-on-windows-vs-native-ubuntu-from-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!