1

I need to create a script that creates the number of files a user specifies, using the name and file ext the user specifies. My loop fails, and only one file is created.

#!/bin/bash

#arguements variables

file=$1
ext=$2
numFiles=$3
count=0

#for loop for 5 repetitions

while [ $3 -ge count ]; do
touch ${1}.${2}.${3}

done

echo " "$3" Files where created using the name "$1" and extension "$2" "

3 Answers 3

2

To create count files with name file and extension ext and suffixed with a number from 1 to count, try:

for i in $(seq "$count"); do touch "$file.$ext.$i"; done
Sign up to request clarification or add additional context in comments.

Comments

1

It is not necessary to specify extension, you could use shell parameter extension. You could also use for loop with index:

#! /bin/bash
FILE_NAME=$1
NUM_OF_FILES=$2 || 0
for ((IDX=0; IDX < NUM_OF_FILES; IDX+=1)) ; do
    # for fname.tar.gz result is fname0.tar.gz, fname1.tar.gz, ...
    touch "${FILE_NAME%%.*}$IDX.${FILE_NAME#*.}"
done

Comments

0

You forgot to increase count. Try this:

#arguements variables

file=$1
ext=$2
numFiles=$3
count=0

#for loop for 5 repetitions

while [ ${numFiles} -ge ${count} ]; do
    touch ${1}.${2}.${count}
    let "count+=1"
done

echo " "$3" Files where created using the name "$1" and extension "$2" "

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.