bash - 如何确定文件是否不可在 bash 脚本中执行

标签 bash shell

我正在遍历一个目录,需要找到所有不可执行的文件。我知道

if [ -x $dir ]; then 
    echo "$dir is an executable file"
fi

显示它是可执行的,但我该如何做相反的事情呢?我试过了

if [ !-x $dir ]; then 
    echo "$dir is not-executable"
fi

但是这不起作用。

最佳答案

通过 Shell Check 运行线路显示:

if [ !-x $dir ]; then
      ^-- SC1035: You are missing a required space here.
         ^-- SC2086: Double quote to prevent globbing and word splitting.

添加缺失的空格和引号会导致:

if [ ! -x "$dir" ]; then

您还可以使用通用语法将 ! 放在方括号外 if !命令,适用于任何命令:

if ! [ -x "$dir" ]; then

关于bash - 如何确定文件是否不可在 bash 脚本中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66554145/

相关文章:

linux - 在 linux 中使用循环更改目录

bash - curl 有效,但不会在 BASH 脚本中执行

linux - Bash:检查命令是否返回特定值

c - 空白行段。错误(自己写shell)

shell - shell/open/command 的正确语法是什么?

bash - 为什么 `ack` 像这样与 `bash` 一起使用时不产生输出?

regex - 如何使用 shell 脚本查找 Linux 发行版名称?

linux - 替换所有不包含匹配字符串的行

mysql - 通过 shell 脚本使用 Ubuntu 终端将 CSV 插入 MySQL

shell - 累积到个人使用awk