我不是在寻找一些高级的ans,只有一行,我正在尝试创建一个可以工作的脚本并使其与递归一起工作并理解它:)
我正在尝试学习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
答案 0 :(得分:1)
如果你真的想使用递归(这在Bash中是一个坏主意):首先,不要递归调用你的脚本。相反,递归调用函数。这将更有效(没有分叉开销)。试图修复你的语法:
#!/bin/bash
shopt -s nullglob
count_x_files() {
# Counts number of executable (by user, regular and non-hidden) files
# in directory given in first argument
# Return value in variable count_x_files_ret
# This is a recursive function and will fail miserably if there are
# too deeply nested directories
count_x_files_ret=0
local file counter=0
for file in "$1"/*; do
if [[ -d $file ]]; then
count_x_files "$file"
((counter+=count_x_files_ret))
elif [[ -f $file ]] && [[ -x $file ]]; then
((++counter))
fi
done
count_x_files_ret=$counter
}
count_x_files "$1"
echo "$count_x_files_ret"