开发者

Bash recursive program

Here is my function so far:

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    
}  

Its echoing

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

It's supposed to create a whole directory, not just one branch. (e.g. hw///* bra开发者_开发百科nch aswell) This is a homework project so I need to use bash.


I believe bash variables are global by default. If you want a function-local variable, you need to use the local keyword.

$ 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜