鸦鸦 发表于 2018-8-25 07:25:39

shell 递归遍历目录,获取目录下的所有内容

#设置全局变量保存数组和数组索引  
export arrayindex
  
export array
  
functionscandir() {
  
local cur_dir parent_dir workdir
  
workdir=$1
  
cd ${workdir}
  
if [ ${workdir} = "/" ]
  
then
  
cur_dir=""
  
else
  
cur_dir=$(pwd)
  
fi
  
for dirlist in $(ls ${cur_dir})
  
do
  
if test -d ${dirlist};then
  
cd ${dirlist}
  
scandir ${cur_dir}/${dirlist}
  
cd ..
  
else
  
# echo ${cur_dir}/${dirlist}
  
#shell 数组赋值
  
array[$arrayindex]=${cur_dir}/${dirlist}
  
#注意shell索引的递增方式
  
((arrayindex++))
  
fi
  
done
  
}
  
function GetALLDirInfo()
  
{
  
local dir
  
dir=$1
  
arrayindex=0
  
if test -d $dir
  
then
  
scandir $dir
  
elif test -f $dir
  
then
  
echo "you input a file not a directory"
  
exit 1
  
else
  
echo "the dir you input $dir is not exit"
  
fi
  
}
  
read -p "please input the dir path:" DIR
  
echo "path is:$DIR"
  
GetALLDirInfo $DIR
  
for content in ${array[@]}
  
do
  
echo $content
  
done


页: [1]
查看完整版本: shell 递归遍历目录,获取目录下的所有内容