[Solved] bash script saving to files
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
[Solved] bash script saving to files
hello!
I have been using bash scripts I have made myself (rather simple ones) and I'm looking for, but haven't found an answer to, saving separate files each time a script is run.
like having; log 1, log 2, log 3.
I think I have encountered an answer before and lost how to translate it into bash, it's described as: if log file 1 exists then log file 2 would be created, so on and so forth.
I have been using bash scripts I have made myself (rather simple ones) and I'm looking for, but haven't found an answer to, saving separate files each time a script is run.
like having; log 1, log 2, log 3.
I think I have encountered an answer before and lost how to translate it into bash, it's described as: if log file 1 exists then log file 2 would be created, so on and so forth.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
We cannot solve our problems with the same thinking we used in creating them - Albert Einstein
Re: bash script saving to files
you could simply use a variable in the log file name with date (and time if needed), then it will always be a different name.
man date
my chosen layouts:
man date
my chosen layouts:
Code: Select all
date '+%d_%m_%Y'
date '+%a_%d_%b_%H.%M'
Re: bash script saving to files
sorry for the late response
can you use that in an example?
like with 'touch'
can you use that in an example?
like with 'touch'
We cannot solve our problems with the same thinking we used in creating them - Albert Einstein
Re: bash script saving to files
16.4. Text Processing Commands
You're either asking how the script can do this for you, or...
How to code shell scripts to write to log files?
via shell script:
If log1 exists, write log2, etc.
you have to code for these requirements.
See "CONDITIONAL EXPRESSIONS" in
You're either asking how the script can do this for you, or...
How to code shell scripts to write to log files?
via shell script:
Code: Select all
export LOG=/path/to/file.log
...
internal|external command >> $LOG
you have to code for these requirements.
See "CONDITIONAL EXPRESSIONS" in
Code: Select all
man bash
Last edited by Habitual on Mon May 18, 2015 12:54 pm, edited 2 times in total.
Re: bash script saving to files
for arbitrary commands you run and on-the-fly logging use:Minty_64 wrote:hello!
I have been using bash scripts I have made myself (rather simple ones) and I'm looking for, but haven't found an answer to, saving separate files each time a script is run.
like having; log 1, log 2, log 3.
I think I have encountered an answer before and lost how to translate it into bash, it's described as: if log file 1 exists then log file 2 would be created, so on and so forth.
Code: Select all
/path/to/my/script.sh &> filename.log
Re: bash script saving to files
example of date command method for new log file name:
create the date command variable
call the variable named TIME like this
so the result would be something like this
create the date command variable
Code: Select all
TIME=$(date '+%a_%d_%b_%H.%M')
Code: Select all
/path/to/File_name_"$TIME".log
Code: Select all
/path/to/File_name_Mon_18_May_18.18.log
Re: bash script saving to files
Minty_64,
To get the log name you want incorporate this sample
Good luck
To get the log name you want incorporate this sample
Code: Select all
#!/bin/bash
d=0
while true ; do
[ -e log.$d ] || break
d=$(($d + 1))
done
echo Next log file would be": log.$d"
exit 0
Re: bash script saving to files
Here's a script that downloads npr's "newscast.mp3" and saves it as unique files with the date:
"newscast.mp3" -> "npr_15.05.22_09.12.51.mp3"
"newscast.mp3" -> "npr_15.05.22_09.12.51.mp3"
Code: Select all
#
# now=$(date +%s )
now=$(date +%y.%m.%d_%H.%M.%S)
filename="npr_$now.mp3"
wget http://public.npr.org/anon.npr-mp3/npr/news/newscast.mp3 --output-document=$filename
#
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Your data and OS are backed up....right?
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: bash script saving to files
Add the following line in your script. Somewhere near the beginning should suffice:Minty_64 wrote:...saving separate files each time a script is run. like having; log 1, log 2, log 3...
Code: Select all
LOGFILENAME="$0".log"$(ls "$0"* | wc -l)"
The above uses the script's file name for the basename, and .log1, .log2, .log3, et al. for the file extension. It's assumed the script's filename is unique in the folder where it's being run.
sample-script
Code: Select all
#!/usr/bin/env bash
LOGFILENAME="$0".log"$(ls "$0"* | wc -l)"
# Log some things
echo "$(date) - Lorem ipsum" >> "$LOGFILENAME"
# Something to do
echo "$(date) - dolor sit amet" >> "$LOGFILENAME"
# Something else to do
echo "$(date) - fun with shell scripts" >> "$LOGFILENAME"
Code: Select all
administrator@dg31pr:~/Desktop$ ls
sample-script
administrator@dg31pr:~/Desktop$ ./sample-script
administrator@dg31pr:~/Desktop$ ./sample-script
administrator@dg31pr:~/Desktop$ ./sample-script
administrator@dg31pr:~/Desktop$ ./sample-script
administrator@dg31pr:~/Desktop$ ./sample-script
administrator@dg31pr:~/Desktop$ ls
sample-script sample-script.log2 sample-script.log4
sample-script.log1 sample-script.log3 sample-script.log5
administrator@dg31pr:~/Desktop$ cat sample-script.log5
Sat May 23 14:12:52 PHT 2015 - Lorem ipsum
Sat May 23 14:12:52 PHT 2015 - dolor sit amet
Sat May 23 14:12:52 PHT 2015 - fun with shell scripts
administrator@dg31pr:~/Desktop$
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: bash script saving to files
that's what I wanted to know Pilosopong Tasyo, thank you!
We cannot solve our problems with the same thinking we used in creating them - Albert Einstein