tags:

views:

56

answers:

2

Hi

I have the following shell script and want to change the filename db_backup.sql to be change so the current date is prepended to it e.g. yyyy-mm-dd-db_backup.sql

#!/bin/sh
mysqldump ... /_sql/db_backup.sql
gzip -f _sql/db_backup.sql

Simple question, hopefully quick and simple answer!

+3  A: 

You can use the date command to do what you want:

#!/bin/sh
fspec=/_sql/$(date +%Y-%m-%d)-db_backup.sql
mysqldump ... ${fspec}
gzip -f ${fspec}

If you're using a shell that doesn't support $(), you may need to use backticks instead:

fspec=/_sql/`date +%Y-%m-%d`-db_backup.sql
paxdiablo
I though he said perl script :(
krico
Thanks - Perfect
Rippo
A: 
use POSIX qw(strftime);
my $file = sprintf("%s-db_backup.sql",  strftime "%Y-%m-%d", localtime);
krico