【问题标题】:How to create multiple files in numbered sequence based on user choice?如何根据用户选择按编号顺序创建多个文件?
【发布时间】:2022-01-09 15:14:18
【问题描述】:

到目前为止,我有这个,我在所选文件夹中创建了我想要的文件夹数量。创建的文件夹数量取决于我的选择。

如何在每个文件夹中创建文件?我希望它们根据我选择的文件数量从 1 开始编号。

read -p "Enter the number of folders to create: " FOLDER_X
read -p "Enter the number of files to create: " FILE_Y

for (( x = 1; x <= FOLDER_X; x++ )); do 
mkdir $DIRY/$x

done

for i in {"$FILE_Y"}
do
echo hello > "$i.txt"
done

【问题讨论】:

    标签: linux bash ubuntu


    【解决方案1】:
    #!/bin/bash
    
    shopt -s extglob
    DIR=/path/to/somewhere
    
    while :; do
        read -p "Enter the number of folders to create: " folders
        read -p "Enter the number of files to create: " files
        [[ $folders,$files == +([[:digit:]]),+([[:digit:]]) ]] && break
        echo "Please provide numeric inputs."
    done
    
    for (( i = 0; i < folders; ++i )); do
        mkdir -p "$DIR/$i" || break
    
        for (( j = 0; j < files; ++j )); do
            echo hello > "$DIR/$i/$j.txt" || break 2
        done
    done
    

    阅读Bash Manual

    【讨论】:

    • 谢谢。仅数字部分很方便。看起来我已经接近了,离目标不太远。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多