Bash递归程序

时间:2011-09-19 13:33:21

标签: bash

到目前为止,这是我的功能:

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。

1 个答案:

答案 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.