2016-01-08 73 views
0

我正在爲Uni工作的一個問題是移動目錄並計算所有文件和目錄以及子目錄中的所有文件和目錄。我無法使用命令find,locate,du或任何遞歸命令(如ls -R)。爲了解決這個問題,我創建了我自己的遞歸命令。我遇到了在每次遞歸中使用相同數組的問題,現在使用類似java的遞歸方式將遞歸中的每個變量分配一個不同的ID。但是這裏使用了相同的變量。我可以從我的輸出中知道它打印出超級目錄中的目錄而不是當前正在使用的目錄。在使用該層次是here,我已經得到了輸出hereBash,用於遞歸的相同變量

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 

    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 
    shopt -u nullglob #Turn off nullglob to ensure it doesn't later interfere 
    echo "${directories[@]}" 
    if [[ "${noDir}" -gt "0" ]]; then 
     for i in "${directories[@]}"; do 
      searchDirectory $i 
     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 .. 
    echo "Increased Values" 
} 
searchDirectory $tgtdir 
echo "Total Files: $visfiles (+$hiddenfiles hidden)" 
echo "Directories Found: $visdir (+$hiddir hidden)" 
echo "Total files and directories: $total" 
exit 0 
+0

如果您使用的是bash,我認爲'local'關鍵字就是您的朋友。 –

+0

聲明你的當地人! –

+0

另外,**認真**,你不能建立一個3線複製?這遠遠超過問題所要求的更多代碼;請參閱http://stackoverflow.com/help/mcve。 –

回答

0

this,你應該聲明directories如使用local局部變量。如果忽略,該變量將是全局的。試一試。

+1

不僅僅是'目錄' - 而是'curdir','noDir','i'和其他所有變量。 –

+0

順便說一句,我強烈建議你避免建議ABS作爲參考;雖然它有很多谷歌果汁,但它經常在其示例中使用不好的做法;我們這些花費時間在freenode的#bash頻道上的人經常會幫助人們忘掉他們在那裏撿到的不良習慣。 –

+0

乾杯,這個排序問題 – HRusby

0

考慮一個簡單的再現:

foo() { 
    var=$1 
    ((var > 10)) && return 
    foo "$(($var * 2))" 
    echo "Passed $var" 
} 

(foo 1) 

因爲在外殼的所有變量都是全局的,除非明確聲明,否則,只要簡單地var=$1,這將發出:

Passed 16 
Passed 16 
Passed 16 
Passed 16 

隨着local vardeclare var,THI旨意正確發出:

Passed 8 
Passed 4 
Passed 2 
Passed 1