2

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///* branch aswell) This is a homework project so I need to use bash.

1
  • What is the output you expect? Commented Sep 19, 2011 at 13:37

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.