判断wsl的版本

代码逻辑

通过判断 cat /proc/version,是否包含wsl等字符来判断wsl的版本。

代码

#!/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"

GitHub 加速计划 / ws / WSL
17.09 K
797
下载
Issues found on WSL
最近提交(Master分支:1 个月前 )
86fa5afa 2 个月前
e899d0b7 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐