Create a binary tree

I need to create a binary tree like structure of directories using shell script... does anyone know of any algorithm for this ?

i tried doing a recursive algorithm

function CreateDir
{
   level=$1
   dirname=$2

   mkdir $dirname/sub1/
   mkdir $dirname/sub2/

   let level=level-1

   if [ $level -gt 0 ]; then
      CreateDir $level $dirname/sub1/
      CreateDir $level $dirname/sub2/
   fi
}
CreateDir 3 testdir

the testdir exists already. the problem is it is not producing a balanced binary tree, it seems to go only one way...if someone can find the mistake and correct it, that would be great too

ps:. this is not a classroom question or something like that, if u have an algorithm that does not use structure, etc.. that would be great too..

thnx in advance

Simple! A little correct quotations here and there and a little streamlining.... you were almost correct already. Here is the correct version (CreateDir() only)

function CreateDir
{

   # set -xv
   typeset -i level="$1"
   typeset    dirname="$2"

   mkdir $dirname/sub1/
   mkdir $dirname/sub2/

   (( level =- 1 )) 

   if [ $level -gt 0 ] ; then
      CreateDir $level "$dirname/sub1"
      CreateDir $level "$dirname/sub2"
   fi
}

I leave it up to you to spot the difference and why this works and your version didn't. ;-)))

Still, i *would* like to give a few hints for successful shell programming:

1 - Be paranoid about quoting! Shells have the notorious habit of digesting whitespace. Be sure to safeguard you variables between as many "-characters as you can afford.

2 - always program type-clean! If you have a variable holding an integer value do NOT use it as string and vice versa! If you need an integer, then DECLARE an integer (by typeset), etc. Shells are very forgiving when you spare the effort, but your program logic will suffer in the long run.

3 - always declare your variables! Yes, you can omit this in most shells, but still you should go the extra way - if you want to use a variable declare it first.

Overall: Just think how you would do it in C, PASCAL, whatever and do the same in shell, even if it isn't absolutely necessary. At least it is good bookkeeping to take not for granted what you use in you program code, but make shure the constructs you are using are really there and in a way you can use them.

bakunin

Thanks bakunin,

that was amazing, I REALLY appreciate your hints, I am already following it. I usually program in java, and i have little shell scripting experience. the syntax and inner workings of the shell scripting is new to me.

Would u recommend any good sites/books to learn and identify mistakes and code good shell scripting ? sorry, if that is too much to ask.

The thing i found with my old code was that the level was being modified randomly by the recursion, thus the level was decreasing much faster and did not complete the tree. I guess the typeset helps in removing it? I am using set -xv, and that is helping a lot to debug my scripts...

I love to learn more, do suggest me some ways if you have time.

Thanks again

  • vijay