Joan Nin
Joan Nin

Reputation: 11

Creating multiple files using script shell

I need to create several files using a shell script. 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.

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)

Upvotes: 0

Views: 2883

Answers (3)

mike futala
mike futala

Reputation: 1

for i in {1..5}
do
  touch "$i.txt"
done 

Upvotes: 0

l0b0
l0b0

Reputation: 58978

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

Upvotes: 2

ivanivan
ivanivan

Reputation: 2225

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

outfile=file1

cat > $outfile << EOF1

Upvotes: 1

Related Questions