How to create a sequence of files whose names come from a numerical sequence with predetermined intervals?

Asked

Viewed 94 times

1

I need to create a sequence of files. Ex :1.log, 2.log, 3.log, 4.log and so on.

I need to define the initial value and the final value, then it creates for me inside the desired folder. Below is the code more or less with the idea I want:

#!/bin/sh
echo -e " DIGITE O NUMERO INICIAL: "
read num1

echo -e " DIGITE AGORA O NUMERO FINAL: ";
read num2

#num3 = num1
#num4 = num2
cd /var/actus/digital/nfce/inutilizar/
touch {$num1..$num2}.log
ls /var/actus/digital/nfce/inutilizar/

It is generating only one file. For example, if you had set num1 equal to 1 and num2 equal to 2, is generating {1..2}.log, and need to generate 1.log and 2.log.

1 answer

1


Just use seq:

for i in $(seq $num1 $num2)
do
    touch $i.log
done

seq $num1 $num2 generates a numerical sequence ranging from $num1 until $num2. Then we make a for to iterate in this sequence, creating the files.

Notice that the command seq is among $( ), because this is the syntax of command substitution, thus the for will be made in the result of seq (which in this case are the numbers).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.