LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 07:24 PM   #1
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

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, 07:46 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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 07:47 PM.
 
Old 07-10-2006, 08: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, 05:55 PM   #4
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:25 AM
filename.part << partially downloaded files... jimjamjahaa Linux - Newbie 2 04-04-2005 12:48 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 02:06 AM
unsetting a part of ENV variable praveenv Linux - Newbie 3 08-28-2004 10:36 AM
put date as part of filename box_l Programming 3 07-27-2004 07:17 AM


All times are GMT -5. The time now is 08:40 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration