LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2006, 06:24 PM   #1
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Rep: Reputation: 30
how to use environment variable as part of filename


occasionally I run a script that somemtimes fails. That it fails is irrelevant as it dumps errors into a .log file. What I'd like to do though is have the script automatically rename the .log file to a unique name that uses the date environment variable as part of the filename.

example: script fails on 07-10-2006 at 19:30.

The script will sense it die, but should
1)copy existing .log file to failed_$date.txt
2)echo 'plz rerun script'

.....aaaaaand if it fails again it doesn't hurt my previously copied off file because it's unique by using the date (and time) as the filename.

Only I don't know how to set it up. Any ideas?
 
Old 07-10-2006, 06:46 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Try

Code:
#! /bin/sh -
NOW=`date %Y%m%d%H%M%S`
cp file.log $NOW.log
echo 'plz rerun script'
--- rod.

Last edited by theNbomr; 07-10-2006 at 06:47 PM.
 
Old 07-10-2006, 07:20 PM   #3
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Quote:
Originally Posted by beeblequix
occasionally I run a script that somemtimes fails. That it fails is irrelevant as it dumps errors into a .log file. What I'd like to do though is have the script automatically rename the .log file to a unique name that uses the date environment variable as part of the filename.

example: script fails on 07-10-2006 at 19:30.

The script will sense it die, but should
1)copy existing .log file to failed_$date.txt
2)echo 'plz rerun script'

.....aaaaaand if it fails again it doesn't hurt my previously copied off file because it's unique by using the date (and time) as the filename.

Only I don't know how to set it up. Any ideas?
I would do this:

Code:
cp CURRENT.log failed_$(date +%F_%T).txt
echo "Please, rerun script"
It's not that theNbomr is wrong, or his code won't work. I just like to condense things a little bit more.

Regards,

Alunduil
 
Old 07-11-2006, 04:55 PM   #4
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Original Poster
Rep: Reputation: 30
wow. thanks gents.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting part of variable p0tw0r Programming 3 04-11-2005 02:25 AM
filename.part << partially downloaded files... jimjamjahaa Linux - Newbie 2 04-04-2005 11:48 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
unsetting a part of ENV variable praveenv Linux - Newbie 3 08-28-2004 09:36 AM
put date as part of filename box_l Programming 3 07-27-2004 06:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration