2011-09-19 60 views
2

这是到目前为止我的功能:猛砸递归程序

function create { 
     a=$3 
     while [ $a -lt $secondnum ] 
     do 
       echo "mkdir $1/$a" 
       if [ $2 -lt $firstnum ] 
       then 
         sum=$(($2+1)) 
         d=$(($a)) 
         create "$1/$d" $sum 0 
       fi 
       a=$(($a+1)) 
     done  
} 

它呼应

mkdir hw1/0 
mkdir hw1/0/0 
mkdir hw1/0/0/0 
mkdir hw1/0/0/1 
mkdir hw1/0/0/2 

它应该创建一个整个目录,不只是一个分支。 (例如hw///* branch aswell)这是一个家庭作业项目,所以我需要使用bash。

+0

您期望的输出是什么? –

回答

1

我相信bash变量默认是全局的。如果你想要一个函数本地变量,你需要使用local关键字。

$ help local 
local: local [option] name[=value] ... 
Define local variables. 

Create a local variable called NAME, and give it VALUE. OPTION can 
be any option accepted by 'declare'. 

Local variables can only be used within a function; they are visible 
only to the function where they are defined and its children. 

Exit Status: 
Returns success unless an invalid option is supplied, an error occurs, 
or the shell is not executing a function.