[英]Shell script to check if running in Windows when using WSL?
我正在嘗試向我的 .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 中鍵入該命令,則該命令有效。 有任何想法嗎?
使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.