在 Ubuntu 上使用 PowerShell 检查文件是否可执行
Check if file is executable using PowerShell on Ubuntu
我不想使用 bash。
我在 FileInfo
对象中查找了任何参数,但没有任何东西可以告诉您文件是否可执行。
我该怎么做?
使用标准 test
utility(Ubuntu 上的 /usr/bin/test
):
$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)
注意:人们希望将 Get-Command
与文件路径一起使用会指示目标文件是否可执行,但从 PowerShell 7.0 开始, 不 是可靠的: 任何 现有文件 - 无论是否可执行 - 当前报告为命令(Application
类型) - 参见 this GitHub issue
# !! SHOULD work, but does NOT as of PowerShell 7.0:
# !! any existing file is reported as executable.
$isExecutable = [bool] (Get-Command -ErrorAction Ignore '/path/to/some/file')
注意:如果此问题得到修复,请注意您始终需要指定 路径 而不是单纯的文件名,即使对于当前位置的文件,例如, Get-Command ./foo
而不是 Get-Command foo
,因为后者只会在 $env:PATH
.
中列出的目录中查找 foo
可执行文件
我不想使用 bash。
我在 FileInfo
对象中查找了任何参数,但没有任何东西可以告诉您文件是否可执行。
我该怎么做?
使用标准 test
utility(Ubuntu 上的 /usr/bin/test
):
$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)
注意:人们希望将 Get-Command
与文件路径一起使用会指示目标文件是否可执行,但从 PowerShell 7.0 开始, 不 是可靠的: 任何 现有文件 - 无论是否可执行 - 当前报告为命令(Application
类型) - 参见 this GitHub issue
# !! SHOULD work, but does NOT as of PowerShell 7.0:
# !! any existing file is reported as executable.
$isExecutable = [bool] (Get-Command -ErrorAction Ignore '/path/to/some/file')
注意:如果此问题得到修复,请注意您始终需要指定 路径 而不是单纯的文件名,即使对于当前位置的文件,例如, Get-Command ./foo
而不是 Get-Command foo
,因为后者只会在 $env:PATH
.
foo
可执行文件