gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:56:57 28 4
gpt4 key购买 nike

我不想使用 bash。

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

我该怎么做?

最佳答案

使用标准test utility (Ubuntu 上的 /usr/bin/test):

$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)

注意:人们期望使用带有文件路径的 Get-Command 来指示目标文件是否可执行,但从 PowerShell 7.0 开始,这可靠: 任何现有文件 - 无论是否可执行 - 当前均报告为命令(类型为 Application) - 请参阅 GitHub issue #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')

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

关于linux - 在 Ubuntu 上使用 PowerShell 检查文件是否可执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61738967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com