2017-05-05 516 views
2

我正在考虑在setup.bash脚本中添加这样的内容。有没有办法在wsl中访问Windows环境变量?

ln -s /mnt/c/Users/Ryan/Downloads $HOME/Downloads 

但显然事实并非总是准确的路径,所以我希望能够做一些

ln -s /mnt/c/Users/%USERNAME%/Downloads $HOME/Downloads 

ln -s %USERPROFILE%/Downloads $HOME/Downloads 

我知道,很明显Windows%vars在bash中不起作用,但是如果wsl可以将这些变量作为$Win32.HOME$Win32.USER或其他东西导出,它将会很酷。

有什么想法?

有没有办法做到这一点?

回答

0

这里是我做过什么:

由于WSL现在有互操作窗口和WSL之间,我把这种优势。

我在叫~/.env.ps1

# Will return all the environment variables in KEY=VALUE format 
function Get-EnvironmentVariables { 
    return (Get-ChildItem ENV: | foreach { "WIN_$(Get-LinuxSafeValue -Value ($_.Name -replace '\(|\)','').ToUpper())='$(Convert-ToWSLPath -Path $_.Value)'" }) 
} 

# converts the C:\foo\bar path to the WSL counter part of /mnt/c/foo/bar 
function Convert-ToWSLPath { 
    param (
     [Parameter(Mandatory=$true)] 
     $Path 
    ) 
    (Get-LinuxSafeValue -Value (($Path -split ';' | foreach { 
     if ($_ -ne $null -and $_ -ne '' -and $_.Length -gt 0) { 
      (((Fix-Path -Path $_) -replace '(^[A-Za-z])\:(.*)', '/mnt/$1$2') -replace '\\','/') 
     } 
    }) -join ':')); 
} 

function Fix-Path { 
    param (
     [Parameter(Mandatory=$true)] 
     $Path 
    ) 
    if ($Path -match '^[A-Z]\:') { 
     return $Path.Substring(0,1).ToLower()+$Path.Substring(1); 
    } else { 
     return $Path 
    } 
} 

# Ouputs a string of exports that can be evaluated 
function Import-EnvironmentVariables { 
    return (Get-EnvironmentVariables | foreach { "export $_;" }) | Out-String 
} 

# Just escapes special characters 
function Get-LinuxSafeValue { 
    param (
     [Parameter(Mandatory=$true)] 
     $Value 
    ) 
    process { 
     return $Value -replace "(\s|'|`"|\$|\#|&|!|~|``|\*|\?|\(|\)|\|)",'\$1'; 
    } 
} 

~/文件夹中有一个PowerShell脚本现在我有,在我.bashrc我有类似如下:

function winenv() { 
    echo $(powershell.exe -Command "Import-Module .\.env.ps1; Import-EnvironmentVariables") | sed -e 's|\r|\n|g' -e 's|^[\s\t]*||g'; 
} 

eval $(winenv) 

一个警告对此,我发现,是我必须在该命令中输入.env.ps1的完整路径。我所做的是编写了一个将wsl样式路径转换回windows路径的函数。然后用它来翻译它。

CMD_DIR=$(wsldir "/mnt/c/Users/$USER/AppData/Local/lxss$HOME/\.env.ps1")

,因为这是加载环境变量的函数,我有硬编码的完整路径,在一定程度上。我确实最终在bash中设置了一个名为LXSS_ROOT=/mnt/c/Users/$USER/AppData/Local/lxss的环境变量,然后使用它。


然后,当我开始一个新的外壳,和我跑env我得到如下:

WIN_ONEDRIVE=/mnt/d/users/rconr/onedrive 
PATH=~/bin:/foo:/usr/bin 
WIN_PATH=/mnt/c/windows:/mnt/c/windows/system32 

,现在我可以添加类似ln -s "$WIN_ONEDRIVE" "~/OneDrive".bashrc

对于示例,你应该这样做:

ln -s $WIN_USERPROFILE/Downloads $HOME/Downloads 

此外,我在我的bin路径中创建了一个名为powershell的脚本。

# gets the lxss path from windows 
function lxssdir() { 
    if [ $# -eq 0 ]; then 
     if echo "$PWD" | grep "^/mnt/[a-zA-Z]/" > /dev/null 2>&1; then 
      echo "$PWD"; 
     else 
      echo "$LXSS_ROOT$PWD"; 
     fi 
    else 
     echo "$LXSS_ROOT$1"; 
    fi 
} 

PS_WORKING_DIR=$(lxssdir) 
if [ -f "$1" ] && "$1" ~= ".ps1$"; then 
    powershell.exe -NoLogo -ExecutionPolicy ByPass -Command "Set-Location '${PS_WORKING_DIR}'; Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content $1))) ${*:2}" 
elif [ -f "$1" ] && "$1" ~!= "\.ps1$"; then 
    powershell.exe -NoLogo -ExecutionPolicy ByPass -Command "Set-Location '${PS_WORKING_DIR}'; Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content $1))) ${*:2}" 
else 
    powershell.exe -NoLogo -ExecutionPolicy ByPass ${*:1} 
fi 
unset PS_WORKING_DIR 

所以我就可以做这样的事情:

$ powershell ~/my-ps-script.ps1
$ powershell -Command "Write-Host 'Hello World'"

我肯定有,可以对所有这一切进行的改进。但是,它目前正在为我的场景工作。

这是我使用的脚本的gist