Purp-E
Purp-E

Reputation: 3

Why does nested for-loop generate wrong numbers?

What i wanted to do

I wanted to create subfolders and within some new subfolders.Their names only contain the numbers 0 and 1. I used nested for-loops for this. Somehow the script generates folders with 3,4,5,6,7,8,9. I cannot find the problem or a solution.

What i already did

I already implemented an echo in every loop which shows the content of the different variables, which was always 0 or 1. Also I put a if statement in which resets a variable back to 0 after the loop is finished. Since echo shows the right content there was no need for a if statement.

DIR=/home/$USER/testdaten
mkdir /home/$USER/testdaten

for a in {0..1}; do
    mkdir $DIR/$( printf %01d $a)
    echo $DIR/$( printf %01d "$a")
    echo $a $b $c $d
    for b in {0..1}; do
        mkdir $DIR/$a/$( printf %02d $a$b)
        echo $DIR/$a/$( printf %02d $a$b)
        echo $a $b $c $d
        for c in {0..1}; do
            mkdir $DIR/$a/$a$b/$( printf %03d $a$b$c)
            echo $DIR/$a/$a$b/$( printf %03d $a$b$c)
            echo $a $b $c $d
            for d in {0..1}; do
                mkdir $DIR/$a/$a$b/$a$b$c/$( printf %04d $a$b$c$d)
                echo $DIR/$a/$a$b/$a$b$c/$( printf %04d $a$b$c$d)      
                echo $a $b $c $d 
            done
        done
    done
done


alternative:

for ((a=0; a<=1; a++)); do
mkdir $DIR/$( printf %01d "$a")
    for ((b=0; b<=1; b++)); do
        mkdir $DIR/$a/$( printf %02d "$a$b")
        for ((c=0; c<=1; c++)); do
            mkdir $DIR/$a/$a$b/$( printf %03d "$a$b$c")
            for ((e=0; e<=1; e++)); do
                mkdir $DIR/$a/$a$b/$a$b$c/$( printf %04d "$a$b$c$e")       
            done
        done
    done
done
/home/$USER/testdaten/0
0
/home/$USER/testdaten/0/00
0 0
/home/$USER/testdaten/0/00/000
0 0 0
/home/$USER/testdaten/0/00/000/0000
0 0 0 0
/home/$USER/testdaten/0/00/000/0001
0 0 0 1
/home/$USER/testdaten/0/00/001
0 0 1 1
/home/$USER/testdaten/0/00/001/0008
0 0 1 0
/home/$USER/testdaten/0/00/001/0009
0 0 1 1
/home/$USER/testdaten/0/01
0 1 1 1
/home/$USER/testdaten/0/01/008
0 1 0 1
mkdir: das Verzeichnis »/home/$USER/testdaten/0/01/010/0064“ kann nicht angelegt werden: Datei oder Verzeichnis nicht gefunden
/home/$USER/testdaten/0/01/010/0064
0 1 0 0
mkdir: das Verzeichnis »/home/$USER/testdaten/0/01/010/0065“ kann nicht angelegt werden: Datei oder Verzeichnis nicht gefunden
/home/$USER/testdaten/0/01/010/0065
0 1 0 1
/home/e$USER/testdaten/0/01/009
0 1 1 1
mkdir: das Verzeichnis »/home/$USER/testdaten/0/01/011/0072“ kann nicht angelegt werden: Datei oder Verzeichnis nicht gefunden
/home/$USER/testdaten/0/01/011/0072
0 1 1 0
mkdir: das Verzeichnis »/home/$USER/testdaten/0/01/011/0073“ kann nicht angelegt werden: Datei oder Verzeichnis nicht gefunden
/home/$USER/testdaten/0/01/011/0073
0 1 1 1
/home/$USER/testdaten/1
1 1 1 1
/home/$USER/testdaten/1/10
1 0 1 1
/home/$USER/testdaten/1/10/100
1 0 0 1
/home/$USER/testdaten/1/10/100/1000
1 0 0 0
/home/$USER/testdaten/1/10/100/1001
1 0 0 1

The error says cannot create the directory: file or directory not found

Upvotes: 0

Views: 59

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15388

Just to elaborate a little, as this has already been solved...

printf "%04d\n" 0101 # leading zero tells %d this input is octal 65
0065

printf "%04s\n" 0101 # %s reads a string (but can still pad with 0's
0101

Upvotes: 0

Purp-E
Purp-E

Reputation: 3

Well i just deleted the parameters %01 etc. in printf and it works now. Refering to the comment of mickp the numbers were seen as octal which then where translated into decimal causing the errors and the other numbers

Octal=decimal
10=8
11=9
100=64
101=65
110=72
111=73

Upvotes: 0

Related Questions