0
function syncfile(){
                echo "[ DONE $file ]"
        return ;
}
function syncfolder(){

        folder=$1
        for foo in `ls -1 $folder`
        do

                file="$folder/$foo"

                        if [ -d $file ];then
                                syncfolder $file
                        elif [ -e $file ];then
                                syncfile $file
                        else
                                echo "$file is neither file nor directory"
                        fi
        done
        return;
}

以上是我的两个递归函数..当我调用 syncfolder $foldername它时,在以下情况下没有给出正确的输出..

假设层次结构如下

portchanges/
portchanges/script/script1/script1.sh
portchanges/script/script1/script2.sh
portchanges/script/script1/script3.sh

portchanges/script/script4.sh
portchanges/script/script5.sh

portchanges/script6.sh
portchanges/script7.sh

portchanges/appl/script11/script11.sh
portchanges/appl/script11/script12.sh
portchanges/appl/script11/script13.sh

现在如果foldername=portchanges 我打电话syncfolder $foldername

它只处理

portchanges/script/script1/script1.sh
portchanges/script/script1/script2.sh
portchanges/script/script1/script3.sh

function syncfile()函数调用......然后它转到函数returnsyncfolder

它将搜索script6.shscript7.shportchanges/script/script1/目录中!这是完全不正当的行为!!

我应该怎么做它对整个文件夹进行递归处理,并且每个文件都可以syncfile()正常工作?

4

2 回答 2

5

folder将变量声明为local。您不希望递归调用更改调用者变量的值。

于 2013-04-02T09:45:21.037 回答
0

以下是在我的情况下工作。如果要打印目录名称,请将其传递给syncfile。

function syncfolder(){
        syncfile $1
        local folder=$1
        for foo in `ls -1 $folder`
        do

                file="$folder/$foo"

                        if [ -d $file ];then
                                syncfolder $file
                        elif [ -e $file ];then
                                syncfile $file
                        else
                                echo "$file is neither file nor directory"
                        fi
        done
        return;
}
于 2013-04-02T09:48:27.093 回答