我已经将这段代码作为一些课程作业的一部分编写,我们必须在不使用 find、du 或 -R 命令的情况下创建递归文件计数器。这似乎有效,但总是将 hiddenfilecounter 返回为 0。是否有任何特殊原因这不起作用。
#!/bin/sh
shopt -s dotglob
directoryCounter=0
fileCounter=0
hiddenDirectoryCounter=0
hiddenFileCounter=0
listAllFiles()
{
local dir=$1
local file
local bn=$(basename $dir)
for file in "$dir"/*; do
if [[ $bn == .* ]]; then
let hiddenDirectoryCounter+=1
listAllFiles "$file"
elif [[ -f $file && "$file" == ^. ]]; then
ls -l $file
let hiddenFileCounter+=1
elif [[ -f $file ]];then
ls -l $file
let fileCounter+=1
elif [[ -d $file ]]; then
listAllFiles "$file"
let directoryCounter+=1
fi
done
}
listAllFiles …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个 bash 脚本,该脚本递归地打印出目录中的所有文件(包括隐藏文件)并记录文件、隐藏文件、隐藏目录和目录的数量。这是作业的一部分,我不能使用-Rorfind或du。
listAllFiles()
{
local dir=$1
local file
directoryCounter=0
fileCounter=0
hiddenFileCounter=0
hiddenDirectoryCounter=0
for file in "$dir"/*; do
if [[ -d $file ]]; then
listAllFiles "$file"
directoryCounter+=1
elif [[ -f $file ]];then
fileCounter+=1]
ls -l $file
elif [[file is a hidden directory]];then
listAllFile "$file"
hiddenDirectoryCounter+=1
elif [[file is a hidden file]];then
hiddenFileCounter+=1
ls -l $file
fi
done
}
Run Code Online (Sandbox Code Playgroud)
有没有办法检测文件/目录是否被隐藏