我正在编写一个需要接受命令行参数的bash脚本,并列出当前目录中的可执行文件数。
因此:$ ./script *.html *.txt
它应显示与这些命令行参数匹配的所有可执行文件的计数,或缺少。
现在我有:
echo -n "All executable files : "
ls -l $* | find . -type f -executable | wc -l
但我得ls: write error: Broken pipe
。
我将如何做到这一点?
答案 0 :(得分:2)
首先,您需要引用脚本的参数,以便在调用脚本之前,模式不会扩展到当前目录中的任何匹配文件。
$ ./script "*.html" "*.txt"
在脚本中,您将使用脚本参数为find
构建一组适当的参数。即使假设解析ls
的输出是一个好主意(事实并非如此),find
也无法读取其标准输入。
to_match=()
for pattern in "$@"; do
to_match+=(-name "$pattern" -o)
done
unset to_match[${#to_match[@]}] # newer versions of bash can use -1 as the index
find . -type f -executable \( "${to_match[@]}" \)
答案 1 :(得分:1)
需要接受命令行参数的bash脚本,并列出当前目录中的可执行文件数。
此bash
函数应与stat
命令配合使用:
lsexecs() {
local c=0
# loop through file globs supplied on command line
for i in $@; do
# read octal permissions for this file
perm=$(stat -c '%a' "0$i")
# perform bitwise AND with 0111 to find executables and count them
((perm & 0111)) && echo "$i" && ((c++))
echo "count: $c"
done
}
现在将此函数称为:
lsexecs '*.sh *.py *.pl'
否则:
lsexecs '*.sh' '*.py' '*.pl'
答案 2 :(得分:1)
在share()
中,您可以执行以下操作:
slideshow
用法:
script
答案 3 :(得分:0)
试试这个
find . -type f -executable -name "*.html" -o -name "*.txt" | wc -l
替代使用:
find . -type f -perm 777 -name "*.html" -o -name "*.txt" | wc -l
find . -type f -perm /u=x,g=x,o=x -name "*.html" -o -name "*.txt" | wc -l
此致
克劳迪奥