2011-09-19 62 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.