File increase

Sorry im really new here this is my second post today!

My question is, im trying to write a script and i want to output to a text file but i want each text file to be different so for instance log.txt, log1.txt, log2.txt ect how would i do that?

this would help you :slight_smile: :slight_smile:

i=1

while [ $i -le 3 ]
do
echo "abcd" > log$i.txt
i=$(($i + 1))
done

Sometimes, you need to have one log file for each call to your script. If this is your case, some people usually add a suffix to the filename with a timestamp. For instance:

(...)
TIMESTAMP=$(date +"%d%m%Y%H%M%S")
LOGFILE=/path/to/logfile/file_${TIMESTAMP}.log
(...)
echo "This is me adding a new line!" >> $LOGFILE
(...)

Hope this helps!
Regards.

Hi,

This is the code im running, now where it says log.txt i want to put a line of code in to create a new file each time and adjust the other log entries

#!/bin/sh
echo STARTING......
date
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
w >>log.txt
echo
echo
echo
echo
echo
"UNZIP FILES"
unzip \*.zip >>log.txt
echo
echo
echo
echo
date >>log.txt
whoami >>log.txt
quota >>log.txt
echo "the task was performed correctly" >>log.txt
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo FINISHED...

what do you mean by "each time"?

i think instead of hardcoding the file name u can parameterize the same by writing a code to take the file name from standard i/p each time u run the script.

here it goes.

#!/bin/sh
echo " enter the file name "
read name
echo STARTING......
date
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
w >> $name
echo
echo
echo
echo
echo
"UNZIP FILES"
unzip \*.zip >>$name
echo
echo
echo
echo
date >>$name
whoami >>$name
quota >>$name
echo "the task was performed correctly" >>$name
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo FINISHED...

i think it works...please reply if it works......as i am also new to scripting

rgds,
jam

Now that I see your last post, I guess you didn't understand one of my answers.
I'm going to modify your code to see if you understand it now :wink:

#!/bin/sh
TIMESTAMP=$(date +"%d%m%Y%H%M%S")
name=/path/to/logfile/file_${TIMESTAMP}.log
echo STARTING......
date
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
w >> $name
echo
echo
echo
echo
echo
"UNZIP FILES"
unzip \*.zip >>$name
echo
echo
echo
echo
date >>$name
whoami >>$name
quota >>$name
echo "the task was performed correctly" >>$name
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo FINISHED...

Now, each time you call this script, a new file with a different name will be created.

Regards.

Thanks im having so much fun learning this it takes me back to the days of qbasic and writing batch files but on a more extreme scale.

Thanks