'Bash Shell Script that every execution will increment 25 .txt files
I'm just a newbie in Bash Scripting and was wondering to do a simple script that will create 25 .txt file in every execution of the shell script. But i think i'm lost because when i execute the shell script 2nd it does not create another 25 .txt file. Please help me.
#!/bin/bash
name="file"
for i in {1..25}
do echo $SORT > $name$i.txt
((i+25))
echo $SORT > $name$i.txt
done;
Solution 1:[1]
If you want to create 25 text files on each execution, those filenames will need to be randomized, otherwise on each execution they will be overwritten.
#!/bin/bash
for i in {1..25}
do
touch $RANDOM.tmp
done
If you wanted to make them sequential, your script will need an external file to save its state between executions:
#!/bin/bash
STATUS_FILE=last.txt
if [ -f "$STATUS_FILE" ]; then
num=$(cat $STATUS_FILE)
fi
for i in {1..25}
do
fn=$(expr $num + $i)
touch $fn.txt
done
echo $fn > $STATUS_FILE
On the first iteration it will create 1.txt ... 25.txt. On the iteration after that, it will generate 26.txt .. 50.txt, and so on.
To restart, just rm last.txt.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy J |
