简体   繁体   English

用于linux的Bash脚本创建5个新文件

[英]Bash script for linux to create 5 new files

I need help in creating a bash script. 我需要帮助来创建一个bash脚本。 I want the script to create 5 new files each time I run it. 我希望每次运行脚本时都会创建5个新文件。

I tried a basic for loop like this: 我尝试了一个像这样的基本for循环:

#!/bin/bash

for i in {0..5}
do
  touch File${i}
done

Obviously, it only creates 5 files named File1 to File5 . 显然,这只是创建了一个名为5个文件, File1File5 I need it to make 5 new files with new names each time I run the script. 每次运行脚本时,我都需要它使用新名称制作5个新文件。 any tips in what direction to look for? 在寻找什么方向的任何提示?

No need to use loop. 无需使用循环。 Simply do this 只需这样做

touch File{1..5}  

To create new files each time based on range 每次根据范围创建新文件

START=$(ls | wc -l)
END=$((START+1))
for((i=START;i<=END;i++)); do  
    touch File$i

This answer was given before it became clear that the names were to be consecutive and following on from the previous run. 这个答案是在明确名称是连续的并且继续上一次运行之前给出的。 I will let it stand rather than delete it as it is potentially informative for other, future readers. 我会让它站起来而不是删除它,因为它可能为其他未来的读者提供信息。

How about with mktemp : mktemp怎么样:

mktemp {a..e}XXXXXX

Sample Output 样本输出

aW1KLgp
bZrPq2I
c1BKn8i
dIjzxrc
ezpWkkO

That will give you one file beginning with a , one beginning with b and so on. 这将为您提供一个以a开头的文件,一个以b开头,依此类推。

Or you may prefer this: 或者你可能更喜欢这个:

mktemp {1..5}-XXXXXX
1-00r8Gf
2-wjojeF
3-lPni6G
4-ihYrOP
5-B5YtEB

Try something like: 尝试类似的东西:

#!/bin/bash

count=$(ls File* 2>/dev/null | wc -l)
for ((i=$count+1; i<=$count+5; i++)); do
    touch $(printf "File%03d" $i)
done

Explanations: 说明:

Here I assume the files start with prefix "File". 这里我假设文件以前缀“File”开头。

The line count=$(ls ...) counts how many files which start with "File" exist in the directory. count=$(ls ...)计算目录中存在以“File”开头的文件数。 The part 2>/dev/null works to suppress the error message when there is no such a file on the initial execution. 2>/dev/null部分2>/dev/null用于在初始执行时没有这样的文件时抑制错误消息。 Then the for loop generates additional five files accordingly. 然后for循环相应地生成另外五个文件。 You can modify the number in %03d depending on how many files you are going to produce. 您可以修改%03d的数字,具体取决于您要生成的文件数量。

While some of the other suggestions use ls piped to wc this isn't guaranteed to be correct, see here. 虽然其他一些建议使用ls piped to wc但这并不保证是正确的,请参阅此处。

Instead you can get the number of files in your current directory as such: 相反,您可以获得当前目录中的文件数量:

files=( * )  
num_files=${#files[@]}  
if [[ $files=='*' ]]; then  
    touch File{000..004}
    exit
fi
for (( i=num_files; i<num_files+5; i++)); do  
    touch $( printf "File%03d" $i )
done

A quick breakdown on what each part does: 快速分解每个部分的作用:
files=( * ) Uses pattern matching to get all the files in your current directory, even if they contain newlines. files=( * )使用模式匹配来获取当前目录中的所有文件,即使它们包含换行符。 The ( ) means to make an array from those files and assign it to files . ( )表示从这些文件生成数组并将其分配给files

${#files[@]} will give the number of elements in the array files , aka how many files are in your directory. ${#files[@]}将给出数组files的元素数量,即目录中有多少文件。

The if block checks if files is litterally * , which means there were no files to match, aka you were in an empty directory, in which case it will create the first 5 files for you. if块检查files是否为litterally * ,这意味着没有匹配的文件,也就是说你在一个空目录中,在这种情况下它会为你创建前5个文件。 Otherwise you iterate over the next 5 file numbers. 否则,您将迭代接下来的5个文件编号。

touch $( printf "File$03d" $i ) executes that printf in a subshell, which will return the name File followed by a 3 digit number with leading zeroes, so that your files will be sorted how you would expect. touch $( printf "File$03d" $i )在子shell中执行printf ,它将返回名称File后跟一个带有前导零的3位数字,以便您的文件按照您的预期排序。

Also you were off by one in your question; 你的问题也是一个人; {0..5} gives you six numbers, not five. {0..5}给你六个数字,而不是五个。

Hope this helps! 希望这可以帮助!

Edit 0: 编辑0:

As Mark said below, using shopt -s nullglob will cause the wildcard to return nothing if it doesn't match any files, which is the desired behavior here. 正如Mark在下面所说,使用shopt -s nullglob将导致通配符在没有匹配任何文件时返回任何内容,这是此处所需的行为。

Here's the updated solution: 这是更新的解决方案:

shopt -s nullglob  
files=( * )  
num_files=${#files[@]}  
for (( i=num_files; i<num_files+5; i++)); do  
    touch $( printf "File%03d" $i )
done

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

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