2016-01-08 30 views
0

我正在处理uni工作的任务,其目标是计算给定目录内的所有文件和目录,然后所有子目录也是如此。我们被禁止使用find,locate,du或任何递归命令(例如ls -R)。 为了解决这个问题,我试图使我自己的递归命令,并在上面运行到错误,更specificly是line 37: testdir/.hidd1/: syntax error: operand expected (error token is ".hidd1/") The Hierarchy I'm using 这种情况的代码如下:Bash - 错误:语法错误:期望操作数(错误标记为“testdir/.hidd1 /”)

tgtdir=$1 
visfiles=0 
hidfiles=0 
visdir=0 
hiddir=0 
function searchDirectory { 
    curdir=$1 

    echo "curdir = $curdir" 
    # Rather than change directory ensure that each recursive call uses the $curdir/NameOfWantedDirectory 
    noDir=$(ls -l -A $curdir| grep ^d | wc -l) # Work out the number of directories in the current directory 
    echo "noDir = $noDir" 

    shopt -s nullglob # Enable nullglob to prevent a null term being added to the array 
    directories=(*/ .*/) # Store all directories and hidden directories into the array 'directories' 
    shopt -u nullglob #Turn off nullglob to ensure it doesn't later interfere 
    echo "${directories[@]}" # Print out the array directories 

    y=0 # Declares a variable to act as a index value 
    for i in $(ls -d ${curdir}*/ ${curdir}.*/); do # loops through all directories both visible and hidden 
     if [[ "${i:(-3)}" = "../" ]]; then 
      echo "Found ./" 
      continue; 
     elif [[ "${i:(-2)}" = "./" ]]; then 
      echo "Found ../" 
      continue; 
     else # When position i is ./ or ../ the loop advances otherwise the value is added to directories and y is incremented before the loop advances 
      echo "Adding $i to directories" 
      directories[y]="$i" 
      let "y++" 
     fi 
    done # Adds all directories except ./ and ../ to the array directories 
    echo "${directories[@]}" 
    if [[ "${noDir}" -gt "0" ]]; then 
     for i in ${directories[@]}; do 
      echo "at position i ${directories[$i]}" 
      searchDirectory ${directories[$i]}  #### <--- line 37 - the error line 
     done # Loops through subdirectories to reach the bottom of the hierarchy using recursion 
    fi 

    visfiles=$(ls -l $tgtdir | grep -v ^total | grep -v ^d | wc -l) 
    # Calls the ls -l command which puts each file on a new line, then removes the line which states the total and any lines starting with a 'd' which would be a directory with grep -v, 
    #finally counts all lines using wc -l 
    hiddenfiles=$(expr $(ls -l -a $tgtdir | grep -v ^total | grep -v ^d | wc -l) - $visfiles) 
    # Finds the total number of files including hidden and puts them on a line each (using -l and -a (all)) removes the line stating the total as well as any directoriesand then counts them. 
    #Then stores the number of hidden files by expressing the complete number of files minus the visible files. 
    visdir=$(ls -l $tgtdir | grep ^d | wc -l) 
    # Counts visible directories by using ls -l then filtering it with grep to find all lines starting with a d indicating a directory. Then counts the lines with wc -l. 
    hiddir=$(expr $(ls -l -a $tgtdir | grep ^d | wc -l) - $visdir) 
    # Finds hidden directories by expressing total number of directories including hidden - total number of visible directories 
    #At minimum this will be 2 as it includes the directories . and .. 
    total=$(expr $visfiles + $hiddenfiles + $visdir + $hiddir) # Calculates total number of files and directories including hidden. 
} 
searchDirectory $tgtdir 
echo "Total Files: $visfiles (+$hiddenfiles hidden)" 
echo "Directories Found: $visdir (+$hiddir hidden)" 
echo "Total files and directories: $total" 
exit 0 

感谢您的帮助,您可以给予

+0

哪条线是#37? –

+1

通过[ShellCheck](http://www.shellcheck.net)运行脚本! – user3439894

回答

0

行37是searchDirectory ${directories[$i]},因为我数。是?

  • 将for循环替换为for i in "${directories[@]}"; do - 添加双引号。这将保持每个元素作为自己的单词。
  • 将第37行替换为searchDirectory "$i"for循环给出了每个元素的数组i,而不是每个索引。因此,你不需要再次进入directories - i已经有你需要的单词。

此外,我注意到第22行和第25行上的echo s被交换:)。

+0

干杯,这已经帮了很多 – HRusby

+0

谢谢,欢迎来到Stack Overflow!您是否请注意(向上三角形)或接受(勾选)此答案,以便其他人知道它有用?另请参阅http://stackoverflow.com/tour上的导览以获取更多信息并获取徽章:)。 – cxw

相关问题