linux - 计算 PATH 中每个变量中的可执行文件数

标签 linux bash shell

我想编写一个脚本来计算变量 PATH 中每个文件夹中的可执行文件的数量。我的代码:

#!/bin/bash

IFS=":"
for directory in $PATH; do
        files=0
        ls -l $directory | while read rights x name group siz m d h name; do
                if [ `echo $rights | cut -c1` = "-" ]; then
                        files=$((${files}+1))
                fi
        done
        echo "Directory ${directory} contains ${files} executable files"
done

我希望回显在 while 循环结束后和下一个 for 循环开始前处理,但它总是打印出文件数 = 0。if 条件内的计数有效。

最佳答案

您是否考虑过使用 find 命令?看这个link .在基于 GNU 的 find 版本上,类似下面的内容应该替换您的 ls 调用和 while 循环。

find $directory -type f -executable -print | wc -l

如果你担心遍历子目录,你可以使用 maxdepth 标志,即

find $directory -maxdepth 1 -type f -executable -print | wc -l

对于 BSD 版本,再次参见 link .

关于linux - 计算 PATH 中每个变量中的可执行文件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966333/

相关文章:

linux - 在 linux 系统上拥有高 pgfree/s 有什么意义?

bash - 如何检查 bash for 循环中的不平等?

linux - ~/.bashrc 中句点 (.) 的用途是什么

linux - 将变量从 SSH 服务器分配给本地机器变量

linux - BASH 嵌套进程替换

linux - 使用作为 bash 脚本参数传递的 glob 表达式

c++ - 如何使用rapidxml创建xml节点

windows - Railo Best 是在 Linux 还是 Windows 上实现的?

c++ - 为测试用例捕获系统日志

linux - 使用文本并用作命令?