2

I am creating a script that will take user input for a file name, and number of files to be created.

The script will then create the number of files required, and use the name given but increment the file name by one each iteration.

So lets say I want 3 files, named boogey. The output in the home folder would be; boogey1.txt, boogey2.txt and boogey3.txt.

Here is the code I have so far.

#!/bin/bash
var1=1
echo -n "Enter the number of your files to create: [ENTER]: "
read file_number
var2=file_number
echo -n "Enter the name of your file: [ENTER]: "
read file_name
var3=file_name
while [ "$var1" -le $file_number]
do 
    cat $file_name.txt
    #var1=$(( var1+1 ))
done
1
  • It generally helps to say what, specifically, you are having problems with when you ask a question. Commented Sep 17, 2014 at 19:02

1 Answer 1

2

Your script is almost working. You just need to:

  1. uncomment that var1 assignment line
  2. add a space between $file_number and ] on the while loop line
  3. change cat to touch (or any of a number of other possibilities) since cat doesn't create files it read them.
  4. Use $file_name$var1.txt in the cat (now touch) line to actually use the incremented index instead of the bare filename.

That being said this script could be dramatically improved. See below for an example.

#!/bin/bash

read -p 'Enter the number of your files to create: [ENTER]: ' file_number
read -p 'Enter the name of your file: [ENTER]: ' file_name

for ((i = 1; i < file_number; i++); do
    : > "$file_name$i.txt"
done
Sign up to request clarification or add additional context in comments.

Comments

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.