在 Ubuntu 上使用 PowerShell 检查文件是否可执行

sch*_*ine 2 linux filesystems powershell executable

我不想使用 bash。

我已经在对象中查找了任何参数FileInfo,但没有任何东西可以告诉您该文件是否可执行。

我该怎么做呢?

mkl*_*nt0 5

使用标准test实用程序/usr/bin/test在 Ubuntu 上):

$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)
Run Code Online (Sandbox Code Playgroud)

注意:人们期望Get-Command与文件路径一起使用将指示目标文件是否可执行,但从PowerShell 7.0 开始这并不可靠:任何现有文件(无论是否可执行)当前都报告为命令(类型为Application)-请参阅GitHub 问题 #12625

# !! 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')
Run Code Online (Sandbox Code Playgroud)

注意:如果这个问题得到解决,请注意,即使对于当前位置的文件,您也始终需要指定路径不仅仅是文件名,例如,Get-Command ./foo而不是Get-Command foo,因为后者只会foo在中列出的目录$env:PATH