判断wsl的版本
代码逻辑
通过判断 cat /proc/version
,是否包含wsl
等字符来判断wsl的版本。文章来源地址https://www.toymoban.com/news/detail-656817.html
代码
#!/bin/bash
# Get the content of /proc/version
proc_version=$(cat /proc/version)
# Check if we are running within WSL
if echo $proc_version | grep -qi 'Microsoft'; then
# Check if it's WSL1 or WSL2
if echo $proc_version | grep -qi 'WSL2'; then
version="WSL2"
else
version="WSL1"
fi
else
version="Linux"
echo "Not running within WSL."
exit 1
fi
# Set host_ip based on WSL version
if [ "$version" == "WSL1" ]; then
host_ip="127.0.0.1"
#echo "export host_ip=127.0.0.1" >> ~/.bashrc
#echo "export DISPLAY=$host_ip:0.0" >> ~/.bashrc
else
host_ip=$(grep nameserver /etc/resolv.conf | cut -f 2 -d " ")
#echo 'host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")' >> ~/.bashrc #对应于WSL2
echo "export DISPLAY=$host_ip:0.0" >> ~/.bashrc
fi
echo "WSL version: $version"
echo "host_ip: $host_ip"
文章来源:https://www.toymoban.com/news/detail-656817.html
到了这里,关于在wsl中判断wsl的版本是wsl1还是wsl2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!