[nflug] bash scripting

Sam Stern samstern at samstern.net
Sat Aug 25 14:20:52 EDT 2007



> -----Original Message-----
> From: nflug-bounces at nflug.org [mailto:nflug-bounces at nflug.org] On Behalf
> Of Robert Stockdale IV
> Sent: Saturday, August 25, 2007 12:55 PM
> To: nflug at nflug.org
> Subject: [nflug] bash scripting
> 
> Hi All:
> I'm a bit rusty at scripting. I've only done a little in the past. What I
> am trying to do is create a file name with the date as part of the name.
> Such as tnt082507.pdf (tnt'mmddyy'.pdf). I did this about 20 years ago in
> MS-DOS. I'm planning to setup a cron job to retrieve a file from the web
> using wget. The file always has the same name and I don't want to
> overwrite the previous days file. I'd like to save them in a dated
> filename with a mv command in a specified directory. How can I
> concatenate the system date as specified to create unique filenames for
> each days file?
> Thank you.
> Bob

Hi Bob,

Here is a quick and dirty way to do it:


#!/bin/bash
export datestamp=`date '+%m-%d-%y-%I-%M-%S-%p'`
tar czf ~/domain.tld-logs-$datestamp.tar.gz logs/domain.tld


NB: The placement of various quotation marks is critical as is the choice of
the correct mark. Man date for information on construction of the date
string. Note the overuse -- down to the second of the date. This helps
sorting out when cron triggerers multiple runs of a script and your logic to
catch this error condition fails for some reason.

HTH

Sam S.
 



More information about the nflug mailing list