0

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)

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

1 Comment

Hello and welcome to Stackoverflow. I noticed you answered a questions which is almost 4 years old with an answer that is already given in another answer. If you think your solution has some benefit, can you explain what advantages it has over the existing solution? In general, it is encouraged to not only post code as an answer but rather explain it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.