简体   繁体   English

使用脚本外壳创建多个文件

[英]Creating multiple files using script shell

I need to create several files using a shell script.我需要使用 shell 脚本创建几个文件。 So far I have this and works.到目前为止,我有这个并且有效。

#!/bin/bash

var1="VALUE 1"
var2="VALUE 2"
ext="cfg"

cat > file1 << EOF1
do some commands on "$var1" 
and/or "$var2"
EOF1

Now I have to create 50 files and the 50 files will have the same text just a few changes and I'm planning to use variables for the changes.现在我必须创建 50 个文件,这 50 个文件将具有相同的文本,只需进行一些更改,我打算使用变量进行更改。

My question is how do I create the 50 files (each file needs to have a specific name but all have the same extension. (.cfg)我的问题是如何创建 50 个文件(每个文件都需要有一个特定的名称,但都具有相同的扩展名。(.cfg)

What determines the file name you will be creating?什么决定了您将创建的文件名? Is it something you can loop through?它是你可以循环的东西吗? Call from an argument to the script?从参数调用脚本?

You can replace file1 with a variable您可以用变量替换 file1

outfile=file1

cat > $outfile << EOF1

Typically you would loop either over a counter:通常,您会在计数器上循环:

for index in $(seq 50)
do
    touch "$index"
done

or over a given list of names:或在给定的名称列表上:

names=(first second third [...])
for name in "${names[@]}"
do
    touch "$name"
done
for i in {1..5}
do
  touch "$i.txt"
done 

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

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