2012-09-19 2 views
0

내 탐색이 작동하지 않는 이유를 찾으려고합니다. 필자는 문제가 "디렉토리가 포함되어있다"는 코드의 지점으로 격리 된 다음 함수에 전달 된 부분을 격리했다고 생각합니다. 이 함수는 새로운 파일 경로를 모두 포함하는 배열을 전달하여 에코합니다. 그러나 어떤 이유 때문에 첫 번째 경로 만 수신합니다. 배열을 잘못 전달했거나 다른 것일 수 있습니까?Bash에서 재귀 적으로 첫 번째 탐색을 수행

#!/bin/bash 

traverse(){ 
    directory=$1 
    for x in ${directory[@]}; do 
    echo "directory contains: " ${directory[@]} 
    temp=(`ls $x`) 
    new_temp=() 
    for y in ${temp[@]}; do 
     echo $x/$y 
     new_temp=(${new_temp[@]} $x/$y) 
    done 
    done 

    ((depth--)) 

    if [ $depth -gt 0 ]; then 
    traverse $new_temp 
    fi 
} 

답변

1

배열을 인수로 전달할 수 없습니다. 문자열 만 전달할 수 있습니다. 배열을 내용의 목록으로 먼저 확장해야합니다. 나는 전역 변수라고 생각하지 않고 함수에 로컬로 depth 을 만들 자유를 취했다.

traverse(){ 
    local depth=$1 
    shift 
    # Create a new array consisting of all the arguments. 
    # Get into the habit of quoting anything that 
    # might contain a space 
    for x in "[email protected]"; do 
    echo "directory contains: [email protected]" 
    new_temp=() 
    for y in "$x"/*; do 
     echo "$x/$y" 
     new_temp+=("$x/$y") 
    done 
    done 

    ((depth--)) 
    if ((depth > 0)); then 
    traverse $depth "${new_temp[@]}" 
    fi 
} 

$ dir=(a b c d) 
$ init_depth=3 
$ traverse $init_depth "${dir[@]}" 
+0

아, 고맙습니다. :) – Sam

관련 문제