我不想使用bash。
我在FileInfo
对象中寻找了任何参数,但是没有什么可以告诉您该文件是否可执行。
我该怎么做?
使用标准test
utility(在Ubuntu上为test
:
/usr/bin/test
注意:有人希望将$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)
与文件路径一起使用将指示目标文件是否可执行,但是从PowerShell 7.0开始,此文件[[not可靠:any现有文件-可执行文件还是可执行文件?不是-当前报告为命令(类型为Get-Command
)-请参见Application
注意:如果要解决此问题,请注意,即使对于当前位置的文件,也将始终需要指定而不是单纯的文件名。 R例如path
# !! 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
中列出的目录中查找foo
可执行文件。