#设置全局变量保存数组和数组索引 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
原文:http://2309998.blog.51cto.com/2299998/1358771