2017-05-05 70 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

相關問題