LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2589|回复: 13

请教:Shell统计某一目录下不同类型文件个数脚本

[复制链接]
发表于 2009-10-20 16:57:35 | 显示全部楼层 |阅读模式
请教高手写个脚本来统计给定目录(含子目录)下不同文件种类的个数的脚本。
如运行:collection.sh /mnt/tmp All
结果:Directory:10 Regular File:20 Link File:0 Pipe:0 Block:0 Char:0 Other:0

我写了个,不过在递归调用时值传递有问题,不解。
  1. #!/bin/bash
  2. #
  3. #Functions:        collection different type of file in the PATH
  4. #Usage:         $0 path_to_collection  type
  5. #
  6. #Date:                2009-10-20
  7. #Note:                for fun....
  8. #Version:      0.0.1
  9. #################<parameters init>#######################
  10. #error numbers:
  11. E_OK=0
  12. E_ABORT=1        #exit for abnormally, common errors
  13. E_ARGUS=2        #Arguments error
  14. E_PATH=3        #path to collection error, noexist or no permission...
  15. E_TYPE=4        #collection type error
  16. #E_UNKNOWN=5        #unknown file types
  17. E_OTHERS=255        #errors unknown
  18. #TYPE to support
  19. arrTYPE=( "Directory" "Regular_file" "Link" "Block" \
  20.         "Character" "Socket" "Pipe" "Others" "All" )
  21. #CMD
  22. CMD_AWK=/usr/bin/awk
  23. #################</parameters init>######################
  24. #################<Functions>############################
  25. Usage(){
  26. #        Function:        print Usage for this script.
  27. #        Usage:                Usage.
  28. #        Input:                 no.
  29. #        output:                Usage :......               
  30.         echo "Usage: $0 Path_to_collection Type_to_collection"
  31.         echo "       Support types: ${arrTYPE[@]}."
  32. }
  33. check_type(){
  34. #        Function:         To check argument $2 in support types
  35. #                        array arrTYPE.
  36. #        Usage:                 check_type $1.
  37. #        Input:                arg1.
  38. #        Output:                1:true, supported.
  39. #                        null:false,Not supported.
  40.         if [ $# != "1" ];then
  41.                 echo "parameter error in function:$0"
  42.                 exit $E_ARGUS
  43.         fi
  44.         local Type=$1
  45.         #check 1:       
  46.         echo " ${arrTYPE[@]} " | $CMD_AWK '/\ '"$Type"'\ /{print "1";}'
  47.         #check 2: <<<<<<<<<<<<<<       
  48.         #flag_find=
  49.         #for arg in ${arrTYPE[@]}
  50.         #do
  51.         #        [ $arg = "$Type" ] && flag_find=1
  52.         #done
  53.         #echo $flag_find
  54.         #>>>>>>>>>>>>>>>>>>>>
  55. }
  56. [b]search_type(){
  57.         local Path=$1
  58.         local Type=$2
  59.         #export arr=( 0 0 0 0 0 0 0 0 )
  60.         [b]/bin/ls $Path |
  61.         while read[/b]
  62.         do
  63.                 local Path2=$Path/$REPLY
  64.                 #echo path2:$Path2
  65.                 if [ -d $Path2 ];then
  66.                         filetype=d
  67.                 else
  68.                         filetype=`/bin/ls -l $Path2|/usr/bin/cut -c 1`                                               
  69.                 fi
  70.                 case $filetype in
  71.                         -)        #arr[0]<-->regular file
  72.                                 let arr[0]++
  73.                                 #echo "Regular:${arr[0]}-${arr[1]}-${arr[2]}-${arr[3]}-${arr[4]}-${arr[5]}-${arr[6]}-${arr[7]}"
  74.                                 continue
  75.                                 ;;
  76.                         d)        #arr[1]<-->directory
  77.                                 let arr[1]++
  78.                                 #echo "00000DIR:${arr[0]}-${arr[1]}-${arr[2]}-${arr[3]}-${arr[4]}-${arr[5]}-${arr[6]}-${arr[7]}"
  79.                                 search_type $Path2 $Type
  80.                                 #echo "+++++DIR:${arr[0]}-${arr[1]}-${arr[2]}-${arr[3]}-${arr[4]}-${arr[5]}-${arr[6]}-${arr[7]}"
  81.                                 continue
  82.                                 ;;
  83.                         l)        #arr[2]<-->link
  84.                                 let arr[2]++
  85.                                 export arr[2]=${arr[2]}
  86.                                 ;;                               
  87.                         p)        #arr[3]<-->pipe
  88.                                 let arr[3]++
  89.                                 ;;
  90.                         c)        #arr[4]<-->character
  91.                                 let arr[4]++
  92.                                 ;;
  93.                         b)        #arr[5]<-->block
  94.                                 let arr[5]++
  95.                                 ;;
  96.                         s)        #arr[6]<-->Socket
  97.                                 let arr[6]++
  98.                                 ;;
  99.                         *)        #arr[7]<-->Others
  100.                                 let arr[7]++
  101.                                 ;;       
  102.                 esac
  103.                 #echo "-----arr1:${arr[0]}-${arr[1]}-${arr[2]}-${arr[3]}-${arr[4]}-${arr[5]}-${arr[6]}-${arr[7]}"
  104.         done
  105.         echo "=====arr2:${arr[0]}-${arr[1]}-${arr[2]}-${arr[3]}-${arr[4]}-${arr[5]}-${arr[6]}-${arr[7]}"
  106. }[/b]
  107. #################</Functions>###########################
  108. #################<Main>#################################
  109. #check arguments
  110. if [ $# != "2" ] && [ $# != "1" ];then
  111.         Usage
  112.         exit $E_ARGUS
  113. fi
  114. PATH=$1
  115. TYPE=$2
  116. #echo --$PATH--$TYPE
  117. if [ ! -z $PATH ] && [ ! -d $PATH ];then
  118.         Usage
  119.         echo "      "Path_to_collection" error!"
  120.         exit $E_ARGUS
  121. fi
  122. if [ $PATH = "." ];then
  123.         PATH=`pwd`
  124.         echo "PATH:$PATH"
  125. fi
  126. if [ `check_type $TYPE` ]; then
  127.         result=`search_type $PATH $TYPE`
  128.         echo "Result:$result |||${arr[@]}"
  129.         exit $E_OK
  130. else
  131.         Usage
  132.         echo "      "Type_to_collection" error!"
  133.         exit $E_ARGUS
  134. fi
  135. #################</Main>#################################
复制代码


这个问题主要在以下2点:
    1 .递归函数(search_type)值如何使用数组返回;
    2 .如何获得返回的值(通过什么形式比较好 ?echo?echo会存在多次返回值的情况)。

希望各位能给小弟一点指点


2009-10-21:

经过下小弟的研究,现已经实现部分功能,只是还不支持含有空格的文件名称:
  1. #!/bin/bash
  2. #
  3. #Functions:        collection different type of file in the PATH
  4. #Usage:         $0 path_to_collection  type
  5. #
  6. #Date:                2009-10-20
  7. #Notice:                for fun....
  8. #                1.BUG:Link directory will be counted to directory(Fixed)
  9. #                2.Do Not support the files that the filenames contain space
  10. #Version:      0.0.2
  11. #
  12. #################<parameters init>#######################
  13. #error numbers:
  14. E_OK=0
  15. E_ABORT=1        #exit for abnormally, common errors
  16. E_ARGUS=2        #Arguments error
  17. E_PATH=3        #path to collection error, noexist or no permission...
  18. E_TYPE=4        #collection type error
  19. #E_UNKNOWN=5        #unknown file types
  20. E_OTHERS=255        #errors unknown
  21. #TYPE to support
  22. arrTYPE=( "Reg" "Dir" "Link" "Pipe" "Char" "Block" "Socket" "Others" "All" )
  23. #CMD
  24. CMD_AWK=/usr/bin/awk
  25. #################</parameters init>######################
  26. #################<Functions>############################
  27. Usage(){
  28. #        Function:        print Usage for this script.
  29. #        Usage:                Usage.
  30. #        Input:                 no.
  31. #        output:                Usage :......               
  32.         echo "Usage: $0 Path_to_collection Type_to_collection"
  33.         echo "       Support types: ${arrTYPE[@]}."
  34. }
  35. check_type(){
  36. #        Function:         To check argument $2 in support types
  37. #                        array arrTYPE.
  38. #        Usage:                 check_type $1.
  39. #        Input:                arg1.
  40. #        Output:                1:true, supported.
  41. #                        null:false,Not supported.
  42.         if [ $# != "1" ];then
  43.                 echo "parameter error in function:$0"
  44.                 exit $E_ARGUS
  45.         fi
  46.         local Type=$1
  47.         #check 1:       
  48.         echo " ${arrTYPE[@]} " | $CMD_AWK '/\ '"$Type"'\ /{print "1";}'
  49.         #check 2: <<<<<<<<<<<<<<       
  50.         #flag_find=
  51.         #for arg in ${arrTYPE[@]}
  52.         #do
  53.         #        [ $arg = "$Type" ] && flag_find=1
  54.         #done
  55.         #echo $flag_find
  56.         #>>>>>>>>>>>>>>>>>>>>
  57. }
  58. search_type(){
  59. #        Function:         To collect files in every types
  60. #        Usage:                 search_type path_to_collect
  61. #        Input:                path_to_collect
  62. #        Output:                To return the number of file which is matched the types
  63. #        Notice:                *1."Link directory" will collection its content of files
  64. #                        *2.Do not support files that filenames contain space
  65.         local Path=$1
  66.         local Type=$2
  67.         # To collect different type files
  68.         #/bin/ls $Path |
  69.         #while read
  70.         arrPath=( `/bin/ls $Path 2>/dev/null` )
  71.         for i_searcg_type in ${arrPath[@]}
  72.         do
  73.                 local Path2="$Path"/"${i_searcg_type}"
  74.                 #echo path2:$Path2
  75.                 if [ -d $Path2 ];then
  76.                         filetype=d
  77.                         [i]tmptype=`/bin/ls -l $Path2 2>/dev/null|/usr/bin/cut -c 1 `
  78.                         [ "$tmptype" = "l" ] && filetype=l[/i]
  79.                 else
  80.                         filetype=`/bin/ls -l $Path2 2>/dev/null|/usr/bin/cut -c 1 `
  81.                 fi
  82.                 case $filetype in
  83.                         d)        #arr[1]<-->directory
  84.                                 [ $Type = "${arrTYPE[1]}" ] && let rtn=${rtn:-0}+1
  85.                                 search_type $Path2 $Type
  86.                                 continue
  87.                                 ;;
  88.                         -)        #arr[0]<-->regular file
  89.                                 [ $Type = "${arrTYPE[0]}" ] && let rtn=${rtn:-0}+1
  90.                                 continue
  91.                                 ;;                       
  92.                         l)        #arr[2]<-->link
  93.                                 [ $Type = "${arrTYPE[2]}" ] && let rtn=${rtn:-0}+1
  94.                                 continue
  95.                                 ;;                               
  96.                         p)        #arr[3]<-->pipe
  97.                                 [ $Type = "${arrTYPE[3]}" ] && let rtn=${rtn:-0}+1
  98.                                 continue
  99.                                 ;;
  100.                         c)        #arr[4]<-->character
  101.                                 [ $Type = "${arrTYPE[4]}" ] && let rtn=${rtn:-0}+1
  102.                                 continue
  103.                                 ;;
  104.                         b)        #arr[5]<-->block
  105.                                 [ $Type = "${arrTYPE[5]}" ] && let rtn=${rtn:-0}+1
  106.                                 continue
  107.                                 ;;
  108.                         s)        #arr[6]<-->Socket
  109.                                 [ $Type = "${arrTYPE[6]}" ] && let rtn=${rtn:-0}+1
  110.                                 continue
  111.                                 ;;
  112.                         *)        #arr[7]<-->Others
  113.                                 [ $Type = "${arrTYPE[7]}" ] && let rtn=${rtn:-0}+1
  114.                                 continue
  115.                                 ;;       
  116.                 esac                 
  117.         done
  118.         return ${rtn:-0}
  119. }
  120. search_and_show_type(){
  121. #        Function:         To collect and show results
  122. #        Usage:                 search_and_show_type Path Type
  123. #        Input:                path_to_collect Type_to_collect
  124. #        Output:                Type:                        Result
  125. #        Notice:                no.
  126. #
  127.         local Path=$1
  128.         local Type=$2       
  129.         case $Type in
  130.                         ${arrTYPE[8]})                               
  131.                                 local Total_file=0
  132.                                 echo "----------------------------------------"
  133.                                 for i_show in 0 1 2 3 4 5 6 7
  134.                                 do
  135.                                         rtn=
  136.                                         #echo "$i $Path ${arrTYPE[$i_show]}"
  137.                                         search_type $Path ${arrTYPE[$i_show]}
  138.                                         let Total_file=$Total_file+${rtn:-0}
  139.                                         echo -e "${arrTYPE[$i_show]}:\t\t\t${rtn:-0}"
  140.                                 done
  141.                                 echo "----------------------------------------"
  142.                                 echo -e "Totals:\t\t\t$Total_file"
  143.                                 ;;
  144.                         ${arrTYPE[0]})
  145.                                 search_type $Path ${arrTYPE[0]}
  146.                                 echo -e "${arrTYPE[0]}:\t${rtn:-0}"
  147.                                 ;;
  148.                         ${arrTYPE[1]})
  149.                                 search_type $Path ${arrTYPE[1]}
  150.                                 echo -e "${arrTYPE[1]}:\t${rtn:-0}"
  151.                                 ;;
  152.                         ${arrTYPE[2]})
  153.                                 search_type $Path ${arrTYPE[2]}
  154.                                 echo -e "${arrTYPE[2]}:\t${rtn:-0}"
  155.                                 ;;
  156.                         ${arrTYPE[3]})
  157.                                 search_type $Path ${arrTYPE[3]}
  158.                                 echo -e "${arrTYPE[3]}:\t${rtn:-0}"
  159.                                 ;;
  160.                         ${arrTYPE[4]})
  161.                                 search_type $Path ${arrTYPE[4]}
  162.                                 echo -e "${arrTYPE[4]}:\t${rtn:-0}"
  163.                                 ;;
  164.                         ${arrTYPE[5]})
  165.                                 search_type $Path ${arrTYPE[5]}
  166.                                 echo -e "${arrTYPE[5]}:\t${rtn:-0}"
  167.                                 ;;
  168.                         ${arrTYPE[6]})
  169.                                 search_type $Path ${arrTYPE[6]}
  170.                                 echo -e "${arrTYPE[6]}:\t${rtn:-0}"
  171.                                 ;;
  172.                         ${arrTYPE[7]})
  173.                                 search_type $Path ${arrTYPE[7]}
  174.                                 echo -e "${arrTYPE[7]}:\t${rtn:-0}"
  175.                                 ;;
  176.                         *)
  177.                                 echo "=====Never Happen!Error type of collect files"
  178.                                 ;;
  179.         esac
  180. }
  181. #################</Functions>###########################
  182. #################<Main>#################################
  183. #check arguments
  184. #if [ $# != "2" ] && [ $# != "1" ];then
  185. if [ $# != "2" ];then       
  186.         Usage
  187.         exit $E_ARGUS
  188. fi
  189. PATH=$1
  190. TYPE=$2
  191. #echo --$PATH--$TYPE
  192. if [ ! -z $PATH ] && [ ! -d $PATH ];then
  193.         Usage
  194.         echo "      "Path_to_collection" error!"
  195.         exit $E_ARGUS
  196. fi
  197. if [ $PATH = "." ];then
  198.         PATH=`pwd`
  199. #        echo "PATH:$PATH"
  200. fi
  201. if [ `check_type $TYPE` ]; then
  202.         search_and_show_type $PATH $TYPE
  203.         exit $E_OK
  204. else
  205.         Usage
  206.         echo "      "Type_to_collection" error!"
  207.         exit $E_ARGUS
  208. fi
  209. #################</Main>#################################
复制代码
结果如下:
  1. ./collections_20091021.sh ./xxx1/ All
  2. ----------------------------------------
  3. Reg:                        3
  4. Dir:                        2
  5. Link:                        2
  6. Pipe:                        0
  7. Char:                        0
  8. Block:                        0
  9. Socket:                        0
  10. Others:                        2
  11. ----------------------------------------
  12. Totals:                        9
复制代码
希望能再完善此脚本,提高脚本执行效率,简化脚本,发现任何问题请指出,谢谢。
还有有以下问题:
1.如何解决文件名含空格的情况?
2.Link的文件夹想归并到Link种类,怎么判断此文件夹为Link的?(使用-d判断为目录)--已解



对了,看到一篇文章相当不错,有关shell递归的,推荐感兴趣的朋友看看:
http://www.ibm.com/developerworks/cn/linux/l-cn-bashrecur/


2009-10-23
今天终于发现问题所在了,下面是0.0.1版的修正版0.0.3,效率较0.0.2要高不少:
  1. #!/bin/bash
  2. #
  3. #Functions:        collection different type of file in the PATH
  4. #Usage:         $0 path_to_collection  type
  5. #
  6. #Date:                2009-10-20
  7. #Note:                for fun....
  8. #Version:        0.0.3
  9. #Release:        for functions return arrays test.
  10. #################<parameters init>#######################
  11. #error numbers:
  12. E_OK=0
  13. E_ABORT=1        #exit for abnormally, common errors
  14. E_ARGUS=2        #Arguments error
  15. E_PATH=3        #path to collection error, noexist or no permission...
  16. E_TYPE=4        #collection type error
  17. #E_UNKNOWN=5        #unknown file types
  18. E_OTHERS=255        #errors unknown
  19. #TYPE to support
  20. arrTYPE=( "Reg" "Dir" "Link" "Pipe" "Char" "Block" "Socket" "Others" "All" )
  21. #CMD
  22. CMD_AWK=/usr/bin/awk
  23. #################</parameters init>######################
  24. #################<Functions>############################
  25. Usage(){
  26. #        Function:        print Usage for this script.
  27. #        Usage:                Usage.
  28. #        Input:                 no.
  29. #        output:                Usage :......               
  30.         echo "Usage: $0 Path_to_collection Type_to_collection"
  31.         echo "       Support types: ${arrTYPE[@]}."
  32. }
  33. check_type(){
  34. #        Function:         To check argument $2 in support types
  35. #                        array arrTYPE.
  36. #        Usage:                 check_type $1.
  37. #        Input:                arg1.
  38. #        Output:                1:true, supported.
  39. #                        null:false,Not supported.
  40.         if [ $# != "1" ];then
  41.                 echo "parameter error in function:$0"
  42.                 exit $E_ARGUS
  43.         fi
  44.         local Type=$1
  45.         #check 1:       
  46.         echo " ${arrTYPE[@]} " | $CMD_AWK '/\ '"$Type"'\ /{print "1";}'
  47.         #check 2: <<<<<<<<<<<<<<       
  48.         #flag_find=
  49.         #for arg in ${arrTYPE[@]}
  50.         #do
  51.         #        [ $arg = "$Type" ] && flag_find=1
  52.         #done
  53.         #echo $flag_find
  54.         #>>>>>>>>>>>>>>>>>>>>
  55. }
  56. search_type(){
  57.         local Path=$1
  58.         local Type=$2
  59. [b]        #/bin/ls $Path |
  60.         #while read
  61.         local i
  62.         for i in `/bin/ls $Path`[/b]
  63.         do
  64.                 local Path2="$Path"/"$i"
  65.                 #echo path2:$Path2
  66.                 if [ -d $Path2 ];then
  67.                         filetype=d
  68.                         tmptype=`/bin/ls -l $Path2 2>/dev/null|/usr/bin/cut -c 1 `
  69.                         [ "$tmptype" = "l" ] && filetype=l
  70.                 else
  71.                         filetype=`/bin/ls -l $Path2|/usr/bin/cut -c 1`
  72.                 fi
  73.                 case $filetype in
  74.                         -)        #arrResult[1]<-->regular file
  75.                                 let arrResult[0]++;;
  76.                         d)        #arrResult[1]<-->directory
  77.                                 let arrResult[1]++
  78.                                 search_type $Path2 $Type
  79.                                 ;;
  80.                         l)        #arrResult[1]<-->link
  81.                                 let arrResult[2]++;;
  82.                         p)        #arrResult[1]<-->pipe
  83.                                 let arrResult[3]++;;
  84.                         c)        #arrResult[1]<-->character
  85.                                 let arrResult[4]++;;
  86.                         b)        #arrResult[1]<-->block
  87.                                 let arrResult[5]++;;
  88.                         s)        #arrResult[1]<-->Socket file
  89.                                 let arrResult[6]++;;
  90.                         *)        #arrResult[1]<-->Others
  91.                                 let arrResult[7]++;;       
  92.                 esac               
  93.         done
  94.         #echo "=====arr:${arrResult[@]}"
  95. }
  96. #################</Functions>###########################
  97. #################<Main>#################################
  98. #check arguments
  99. if [ $# != "2" ] && [ $# != "1" ];then
  100.         Usage
  101.         exit $E_ARGUS
  102. fi
  103. PATH=$1
  104. TYPE=$2
  105. #echo --$PATH--$TYPE
  106. if [ ! -z $PATH ] && [ ! -d $PATH ];then
  107.         Usage
  108.         echo "      "Path_to_collection" error!"
  109.         exit $E_ARGUS
  110. fi
  111. if [ $PATH = "." ];then
  112.         PATH=`pwd`
  113.         #echo "PATH:$PATH"
  114. fi
  115. if [ `check_type $TYPE` ]; then
  116.         arrResult=( 0 0 0 0 0 0 0 0 )
  117.         iTotal=0
  118.         search_type $PATH $TYPE
  119.         case $TYPE in
  120.                 ${arrTYPE[8]})
  121.                         echo "----------------------------------------"
  122.                         for k in 0 1 2 3 4 5 6 7
  123.                         do
  124.                                 echo -e "${arrTYPE[$k]}:\t\t${arrResult[$k]}"
  125.                                 let iTotal=$iTotal+${arrResult[$k]}
  126.                         done
  127.                         echo "----------------------------------------"
  128.                         echo -e "Total:\t\t$iTotal"
  129.                         echo "----------------------------------------"
  130.                         ;;
  131.                 ${arrTYPE[0]})
  132.                         echo -e "${arrTYPE[1]}:\t\t${arrResult[0]}";;
  133.                 ${arrTYPE[1]})
  134.                         echo -e "${arrTYPE[1]}:\t\t${arrResult[1]}";;
  135.                 ${arrTYPE[2]})
  136.                         echo -e "${arrTYPE[2]}:\t\t${arrResult[2]}";;
  137.                 ${arrTYPE[3]})
  138.                         echo -e "${arrTYPE[3]}:\t\t${arrResult[3]}";;
  139.                 ${arrTYPE[4]})
  140.                         echo -e "${arrTYPE[4]}:\t\t${arrResult[4]}";;
  141.                 ${arrTYPE[5]})
  142.                         echo -e "${arrTYPE[5]}:\t\t${arrResult[5]}";;
  143.                 ${arrTYPE[6]})
  144.                         echo -e "${arrTYPE[6]}:\t\t${arrResult[6]}";;
  145.                 ${arrTYPE[7]})
  146.                         echo -e "${arrTYPE[7]}:\t\t${arrResult[7]}";;
  147.                 *)
  148.                         echo "=====Never Happen!Error type of collect files"
  149.                         ;;
  150.         esac       
  151.         exit $E_OK
  152. else
  153.         Usage
  154.         echo "      "Type_to_collection" error!"
  155.         exit $E_ARGUS
  156. fi
  157. #################</Main>#################################
复制代码

见0.0.3版代码加粗部分,将"/bin/ls $Path | while read"换成for循环就不存在值传递问题,不解,为什么?期待高人解答。。。。。。
 楼主| 发表于 2009-10-20 18:07:22 | 显示全部楼层
没人回复吗??
回复 支持 反对

使用道具 举报

发表于 2009-10-20 20:54:00 | 显示全部楼层
Post by eeslook;2038462
没人回复吗??

将问题精简一些,也许容易明白。
这其实是一个遍历目录树的问题。围绕这个焦点,就容易解决。
回复 支持 反对

使用道具 举报

发表于 2009-10-20 22:19:40 | 显示全部楼层
楼主,我还是想说我的想法。

这个命题,用Shell外加几个命令(find, wc, awk),可以很容易解决的。。。

举个例子吧:看当前目录下  软链接  的个数:
  1. find . -type l | wc | awk '{print $1}'
复制代码
回复 支持 反对

使用道具 举报

发表于 2009-10-20 22:32:19 | 显示全部楼层
  1. num=0
  2. for in in `find ./ -type f -print`
  3. do
  4.         num=$(($num+1))
  5. done
  6. echo $num
复制代码


http://bbs3.chinaunix.net/thread-1115557-1-1.html
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-21 11:08:49 | 显示全部楼层
Post by zswlb9999;2038531
将问题精简一些,也许容易明白。
这其实是一个遍历目录树的问题。围绕这个焦点,就容易解决。


是的,就是个遍历目录树的问题,怎样把结果传回去?数组变量定义有问题吗?
还有就是最后我怎样获得结果,函数怎么返回我想要的结果(数组arr)。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-21 11:11:16 | 显示全部楼层
Post by tomgrean;2038560
楼主,我还是想说我的想法。

这个命题,用Shell外加几个命令(find, wc, awk),可以很容易解决的。。。

举个例子吧:看当前目录下  软链接  的个数:

  1. find . -type l | wc | awk '{print $1}'
复制代码


是的,我有知道find的方法是可以的,但是这个问题主要是目录树遍历的问题和值返回的问题,我想解决我碰到的这个问题的。
另外,find并不是都支持的,写个通用的脚本会更好点。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-21 11:16:52 | 显示全部楼层
Post by apzc2529;2038565
  1. num=0
  2. for in in `find ./ -type f -print`
  3. do
  4.         num=$(($num+1))
  5. done
  6. echo $num
复制代码



http://bbs3.chinaunix.net/thread-1115557-1-1.html


谢谢,我想解决我遇到的值传递的问题,关键不是递归的实现,关键是在递归过程中统计处的各个值传递回去的问题,我最后得到的结果不正确。
回复 支持 反对

使用道具 举报

发表于 2009-10-22 09:06:01 | 显示全部楼层
1)支持楼主探索。提供一些思路参考:
2)可以考虑用数组模拟堆栈。C之类的语言就是用堆栈实现递归功能的。
这个手册有模拟堆栈的介绍
http://www.linuxsir.cn/bbs/thread256887.html
3)可以在函数中可以考虑传递变量名(或数组)名,来传递变量(全局)。(间接引用)
曾经试过。
设$1是ck
c=$1
eval 'b=${'$c'}'
效果b=${ck}

http://www.linuxsir.cn/bbs/thread318428.html
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-22 13:32:45 | 显示全部楼层
Post by zswlb9999;2038886
1)支持楼主探索。提供一些思路参考:
2)可以考虑用数组模拟堆栈。C之类的语言就是用堆栈实现递归功能的。
这个手册有模拟堆栈的介绍
http://www.linuxsir.cn/bbs/thread256887.html
3)可以在函数中可以考虑传递变量名(或数组)名,来传递变量(全局)。(间接引用)
曾经试过。
设$1是ck
c=$1
eval 'b=${'$c'}'
效果b=${ck}

http://www.linuxsir.cn/bbs/thread318428.html


十分感谢,开始我是准用数组传递的,这样效率要高多了,可是在递归返回数组值时没有找到解决的办法,递归返回的数组在递归调用返回过程中出现了问题:
我写的第一个版本(0.0.1),返回如下结果:

  1. ./collections_20091021.sh . All
  2. Result:00000DIR:27-1------
  3. +++++DIR:27-1------
  4. 00000DIR:27-2------
  5. +++++DIR:27-2------
  6. 00000DIR:27-3------
  7. +++++DIR:27-3------
  8. =====arr2:------- |||
复制代码


第2个版本(0.0.2),同样的环境结果(此为正确结果)为:

  1. ./collections_20091021.sh . All
  2. ----------------------------------------
  3. Reg:                        31
  4. Dir:                        7
  5. Link:                        3
  6. Pipe:                        0
  7. Char:                        0
  8. Block:                        0
  9. Socket:                        0
  10. Others:                        0
  11. ----------------------------------------
  12. Totals:                        41
复制代码

还是迷糊在数组中,我学习下你给的资料,然后再试试。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表