bash 使用脚本在bash中创建文件的多个副本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19598173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating multiple copies of a file in bash with a script
提问by Tom Bennett
I am starting to learn how to use bash shell commands and scripting in Linux.
我开始学习如何在 Linux 中使用 bash shell 命令和脚本。
I want to create a script that will take a source file, and create a chosen number of named copies.
我想创建一个脚本来获取源文件,并创建选定数量的命名副本。
for example, I have the source as testFile, and I choose 15 copies, so it creates testFile1, 2, 3 ... 14, 15 in the same location.
例如,我将源作为 testFile,我选择了 15 个副本,因此它在同一位置创建了 testFile1, 2, 3 ... 14, 15。
To try and achieve this I have tried to make the following command:
为了尝试实现这一点,我尝试执行以下命令:
for LABEL in {$X..$Y}; do cp $INPUT $INPUT$LABEL; done
However, instead of creating files from X to Y, it makes just one file with (for example) {1..5} appended instead of files 1, 2, 3, 4 and 5
但是,它不是创建从 X 到 Y 的文件,而是仅生成一个附加了(例如){1..5} 的文件,而不是文件 1、2、3、4 和 5
How can I change it so it properly uses the variable as a number for the loop?
如何更改它以便正确使用变量作为循环的数字?
回答by Jonathan Leffler
The brace expansionmechanism is a bit limited; it doesn't work with variables, only literals.
所述支架膨胀机构是有点限制; 它不适用于变量,仅适用于文字。
For what you want, you probably have the seq
command, and could write:
对于你想要的,你可能有seq
命令,并且可以写:
INPUT=testFile
for num in $(seq 1 15)
do
cp "$INPUT" "$INPUT$num"
done
回答by Gilles Quenot
Using a C-style for loop:
使用C 风格的 for 循环:
$ x=0 y=15
$ for ((i=x; i<=y; i++)); do cp "$INPUT" "$INPUT$i"; done