[Solved] bash script saving to files

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Minty_64
Level 3
Level 3
Posts: 101
Joined: Mon Jun 30, 2014 3:18 am

[Solved] bash script saving to files

Post by Minty_64 »

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.
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.
We cannot solve our problems with the same thinking we used in creating them - Albert Einstein
BlackVeils

Re: bash script saving to files

Post by BlackVeils »

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:

Code: Select all

date '+%d_%m_%Y'

date '+%a_%d_%b_%H.%M'
User avatar
Minty_64
Level 3
Level 3
Posts: 101
Joined: Mon Jun 30, 2014 3:18 am

Re: bash script saving to files

Post by Minty_64 »

sorry for the late response
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
Habitual

Re: bash script saving to files

Post by Habitual »

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:

Code: Select all

export LOG=/path/to/file.log
...
internal|external command >> $LOG
If log1 exists, write log2, etc.
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.
Habitual

Re: bash script saving to files

Post by Habitual »

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.
for arbitrary commands you run and on-the-fly logging use:

Code: Select all

/path/to/my/script.sh &> filename.log
else, code for it.
BlackVeils

Re: bash script saving to files

Post by BlackVeils »

example of date command method for new log file name:

create the date command variable

Code: Select all

TIME=$(date '+%a_%d_%b_%H.%M')
call the variable named TIME like this

Code: Select all

/path/to/File_name_"$TIME".log
so the result would be something like this

Code: Select all

/path/to/File_name_Mon_18_May_18.18.log
WharfRat

Re: bash script saving to files

Post by WharfRat »

Minty_64,

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
Good luck :wink:
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: bash script saving to files

Post by Flemur »

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"

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?
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: bash script saving to files

Post by Pilosopong Tasyo »

Minty_64 wrote:...saving separate files each time a script is run. like having; log 1, log 2, log 3...
Add the following line in your script. Somewhere near the beginning should suffice:

Code: Select all

LOGFILENAME="$0".log"$(ls "$0"* | wc -l)"
When you want to send text to the logfile anywhere in your script, just redirect it to $LOGFILENAME. The next time you run the script, a new logfile will be used.

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"
With "sample-script" as file name, running it several times in a row should produce:

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].
User avatar
Minty_64
Level 3
Level 3
Posts: 101
Joined: Mon Jun 30, 2014 3:18 am

Re: bash script saving to files

Post by Minty_64 »

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
Locked

Return to “Scripts & Bash”