【发布时间】:2015-01-31 11:34:08
【问题描述】:
我不是在寻找一些高级别的答案,只有一行,我正在尝试制作一个可以工作的脚本,并使其与递归一起工作并理解它:)
我正在尝试学习 bash 脚本,但遇到了一些问题:
我正在尝试计算目录及其子目录中可执行文件的数量
这就是我的想法
文件名countFiles:
#!/bin/bash
counter = 0
for FILE in ($1/*) /// going over all the files in the input path
do
if (-d $FILE); then /// checking if dir
./countFiles $FILE /// calling the script again with ned path
counter= $($counter + $0) /// addint to the counter the number of exe files from FILE
fi
if (-f $FILE); then /// checking if file
if (-x $FILE); then /// checking id exe file
counter = $counter + 1 // adding counter by 1
fi
fi
done
exit($counter) // return the number for exe files
【问题讨论】:
-
这不是 Bash!现在,这不行吗?
find . -type f -exec sh -c 'for i; do [ -x "$i" ] && printf 1; done' _ {} \; | wc -c -
是的,它可以工作,但我正在尝试制作一个易于理解的脚本并制作类似的脚本
-
呃...为什么不
find . -type f -executable | wc -l? -
@Wintermute:因为在文件名包含换行符的情况下(可能不太可能)失败:
touch $'a\nb'; chmod +x $'a\nb'; find . -type f -executable | wc -l。-executable也不是 POSIX(我猜它只在 GNUfind中可用)。除此之外,你的命令很好。 -
@gniourf_gniourf 是的,好的。我猜贝壳产卵只是以错误的方式摩擦我。也许
find . -type f -exec test -x '{}' \; -printf 1 | wc -c?-exec是毕竟只是另一个过滤器。