You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌套for循环生成非0/1数字目录?目录创建失败问题求助

Hey there! Let's break down what's going wrong with your script and fix it up properly.

What's Causing the Issues?

The two problems you're facing (non-0/1 directory names and "file or directory not found" errors) both stem from how the shell interprets your variable concatenation and mismatched directory paths:

1. Why You're Seeing Numbers Like 8, 9, or 64

When you write something like $a$b or $a$b$c, the shell treats that combined string as a number. If the string starts with a 0, the shell assumes it's an octal (base-8) number instead of decimal. For example:

  • 010 in octal translates to 8 in decimal, 011 becomes 9, and 064 becomes 52
  • When you pass these octal numbers to printf %0Nd, it converts them to decimal and pads with zeros—hence those random non-0/1 digits in your directory names.

2. Why the "File Not Found" Errors Happen

Look at your third loop:

mkdir $DIR/$a/$a$b/$( printf %03d $a$b$c)

This creates a directory named something like 008 (because 010 octal becomes 8 decimal, padded to 3 digits). But in the fourth loop, you try to create a directory inside $a$b$c—which is the raw string 010, not the 008 you actually created. The parent directory 010 doesn't exist, so you get the error.


Fixed Scripts

We need to treat your variables as strings instead of numbers, so the shell doesn't try to parse them as octal values. Here are two solid ways to do this:

Method 1: Use printf with Multiple Arguments (Clear & Explicit)

This uses printf to concatenate each variable as a string, avoiding octal parsing entirely:

DIR="/home/$USER/testdaten"
mkdir -p "$DIR"  # -p creates missing parent dirs and ignores existing ones

for a in {0..1}; do
    dir1=$(printf "%d" "$a")
    mkdir -p "$DIR/$dir1"
    echo "$DIR/$dir1"
    echo "$a $b $c $d"

    for b in {0..1}; do
        dir2=$(printf "%d%d" "$a" "$b")
        mkdir -p "$DIR/$dir1/$dir2"
        echo "$DIR/$dir1/$dir2"
        echo "$a $b $c $d"

        for c in {0..1}; do
            dir3=$(printf "%d%d%d" "$a" "$b" "$c")
            mkdir -p "$DIR/$dir1/$dir2/$dir3"
            echo "$DIR/$dir1/$dir2/$dir3"
            echo "$a $b $c $d"

            for d in {0..1}; do
                dir4=$(printf "%d%d%d%d" "$a" "$b" "$c" "$d")
                mkdir -p "$DIR/$dir1/$dir2/$dir3/$dir4"
                echo "$DIR/$dir1/$dir2/$dir3/$dir4"
                echo "$a $b $c $d"
            done
        done
    done
done

Method 2: Direct String Concatenation (Simpler)

Since a, b, c, d only ever hold 0 or 1, we can just concatenate them directly as strings:

DIR="/home/$USER/testdaten"
mkdir -p "$DIR"

for a in {0..1}; do
    dir1="$a"
    mkdir -p "$DIR/$dir1"
    echo "$DIR/$dir1"

    for b in {0..1}; do
        dir2="$a$b"
        mkdir -p "$DIR/$dir1/$dir2"
        echo "$DIR/$dir1/$dir2"

        for c in {0..1}; do
            dir3="$a$b$c"
            mkdir -p "$DIR/$dir1/$dir2/$dir3"
            echo "$DIR/$dir1/$dir2/$dir3"

            for d in {0..1}; do
                dir4="$a$b$c$d"
                mkdir -p "$DIR/$dir1/$dir2/$dir3/$dir4"
                echo "$DIR/$dir1/$dir2/$dir3/$dir4"
            done
        done
    done
done

Key Fixes Explained
  1. mkdir -p is essential: This flag automatically creates any missing parent directories and won't throw an error if the directory already exists—saves you a ton of path-related headaches.
  2. Avoid octal parsing: By treating variables as strings (either via direct concatenation or printf's multi-argument syntax), we stop the shell from interpreting 0-prefixed strings as octal numbers.
  3. Consistent paths: Each directory name is stored in a variable (like dir1, dir2) that's reused in subsequent loops. This ensures the parent directory you're trying to write to is exactly the one you created earlier.

Run either of these scripts, and you'll get nested directories made up solely of 0s and 1s, with no more "file not found" errors.

内容的提问来源于stack exchange,提问作者Purp-E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:05:40