开发者

prepend date to a filename in a shell script

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!


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


use POSIX qw(strftime);
my $file = sprintf("%s-db_backup.sql",  strftime "%Y-%m-%d", localtime);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜