简体   繁体   English

如何在 linux 中使用 for 循环创建脚本,以不同方式编辑文件

[英]How can I create a script with for loop in linux that would edit files differently

I have 10 files and I need a script that can, for example, write numbers from 1 to 10 in each file so that in one file would be written one number.我有 10 个文件,我需要一个脚本,例如,可以在每个文件中写入 1 到 10 的数字,以便在一个文件中写入一个数字。 I tried doing like this, but it doesn't work:我试过这样做,但它不起作用:

    #!/bin/bash
    
    for (( i=1; i<=10; i++ )); do
    cat file$i
    $i
    done

Do you mean each file, say 1.txt , contains 1 in it?你是说每个文件,比如1.txt ,都包含1吗? Then this may work:那么这可能会起作用:

 #!/bin/bash
    
for i in {1..10}; do
     echo $i > ${i}.txt
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM