首页 > 其他 > 详细

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

时间:2014-02-14 07:34:54      阅读:328      评论:0      收藏:0      [点我收藏+]
#设置全局变量保存数组和数组索引
export arrayindex
export array
function  scandir() {
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

 

本文出自 “风清扬song” 博客,请务必保留此出处http://2309998.blog.51cto.com/2299998/1358771

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

原文:http://2309998.blog.51cto.com/2299998/1358771

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!