は私がやったことです:
WSLが今の窓とWSL間の相互運用機能を持っているので、私はそれを利用しました。
私は今、私は私の.bashrc
に、私は次のようなものを持っている、ということがあること~/.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スクリプトを持っている:
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")
これは、環境変数をロードする機能ですので、私はある程度、ハードコードへの完全なパスを持っています。私は実際には、LXSS_ROOT=/mnt/c/Users/$USER/AppData/Local/lxss
と呼ばれるbashの環境変数を設定し、それを使用して終了しました。私は新しいシェルを起動し、私はenv
を実行したときに
その後、私は次の取得:
WIN_ONEDRIVE=/mnt/d/users/rconr/onedrive
PATH=~/bin:/foo:/usr/bin
WIN_PATH=/mnt/c/windows:/mnt/c/windows/system32
と私は今、あなたの例のために私の.bashrc
にln -s "$WIN_ONEDRIVE" "~/OneDrive"
のようなものを追加することができますこれを行う:
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があります。