将 Windows 路径更改为 unix 路径

use*_*975 2 powershell

我有一个脚本,它定义了一个值为“X:\data\tables\”的变量 $winpath。此路径值不能在另一个函数使用时更改,但是是否可以将此值转换/转换为 unix 方式(使用某些分隔符或任何其他方法?)并存储在另一个变量中?即 /x/data/tables

$server = 10.1.1.1
$user = jason
$winpath = X:\data\tables\
Write-Output $user + '@' + $server + ':' + $winpath
Run Code Online (Sandbox Code Playgroud)

期望输出-

jason@10.1.1.1/x/data/tables
Run Code Online (Sandbox Code Playgroud)

sod*_*low 5

干得好:

$server = "10.1.1.1"
$user = "jason"
$winpath = "X:\data\tables\"

# replace backslashes with slashes, colons with nothing,
# convert to lower case and trim last /
$nixPath = (($winpath -replace "\\","/") -replace ":","").ToLower().Trim("/")

"$user@$server/$nixpath"
Run Code Online (Sandbox Code Playgroud)

变量在双引号中展开,因此您可以直接构建字符串。