【问题标题】:Shell script to check if running in Windows when using WSL?使用 WSL 时检查是否在 Windows 中运行的 Shell 脚本?
【发布时间】:2020-07-10 08:29:30
【问题描述】:

我正在尝试向我的 .zshrc 文件添加一个条件,该条件将初始化一些我只希望在我在 Linux 的 Windows 子系统中发生的配置内容。我试过了,但没有运气:

if [ "$('cmd.exe /c "systeminfo" | grep "^OS Name"')" =~ "Windows" ]; then
        echo "windows baby!"
fi

这给出了:

no such file or directory: cmd.exe /c "systeminfo" | grep "^OS Name"

...但是如果我直接在 shell 中键入该命令,它就可以工作。 有什么想法吗?

【问题讨论】:

  • 我没用过WSL,所以随便猜一下:当你在WSL的时候uname会说什么?
  • 看/etc/profile的头部有勾选。用同样的方法做就行了
  • @user1934428 "Linux" :/
  • @MatthewMasurka :而且uname -a 也没有显示出重要的东西?当然,Carlos Rafael Ramirez 的想法看起来更有希望。

标签: windows shell zsh windows-subsystem-for-linux


【解决方案1】:

使用uname -r 可以解决问题

根据https://github.com/microsoft/WSL/issues/423#issuecomment-608236476,如果你使用

uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip'

如果是 WSL,您将获得“Microsoft”的输出。否则,您应该没有输出。

所以你可以使用类似的东西

if [ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ];
then
        echo "This is Windows WSL baby!"
else
        echo "Not Windows"
fi

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多