gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:54 27 4
gpt4 key购买 nike

我想编写一个脚本来计算变量 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/

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