开发者

Bash recursive program

开发者 https://www.devze.com 2023-04-05 16:48 出处:网络
Here is my function so far: function create { a=$3 while [$a -lt $secondnum ] do echo \"mkdir $1/$a\" if [ $2 -lt $firstnum ]

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

精彩评论

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

关注公众号