我正在编写调用其他 PowerShell 脚本的 PowerShell 脚本,& .\other\script.ps1
但我认为反斜杠\
是 Windows 的东西,我希望我的脚本能够跨平台工作。
我注意到 Windows 中的 PowerShell 确实接受,& ./other/script.ps1
但这可能是偶然的。PowerShell 总是会将我的反斜杠转换为主机平台吗?
在 PowerShell 中跨平台处理路径分隔符的最佳方法是什么?
根据我的经验,Windows PowerShell 很乐意接受\
或/
作为路径分隔符,因此跨平台的一种方法是始终使用/
.
或者,您可以使用Path
cmdlet 来为您处理构建路径,例如:
Join-Path -Path $Pwd -ChildPath (Join-Path -Path 'Other' -ChildPath 'script.ps1')
Run Code Online (Sandbox Code Playgroud)
或者获取当前目录中文件的路径:
Resolve-Path test.txt
Run Code Online (Sandbox Code Playgroud)
路径 cmdlet:
~> get-command *-path* | Select Name
Name
----
Convert-Path
Join-Path
Resolve-Path
Split-Path
Test-Path
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3593 次 |
最近记录: |