Putting the date in DD-MM-YYYY format into a variable
Hi, I need to get the date in the above format into a variable so my backup script can create a directory to store the archive in. any suggestions? Thanks Ray
On Friday 08 March 2002 13.12, Ray Booysen wrote:
Hi, I need to get the date in the above format into a variable so my backup script can create a directory to store the archive in. any suggestions?
mkdir /var/backup/`date +%d-%m-%Y` is one way to do it. I'm sure there are others //Anders
On Fri, Mar 08, 2002 at 02:12:40PM +0200, Ray Booysen wrote:
Hi, I need to get the date in the above format into a variable so my backup script can create a directory to store the archive in. any suggestions?
See "man date" for future reference. Here's what you are after: the_date=`date +%d-%m-%Y` The back-ticks cause the output of the command between them (in this case, the date command) to be returned as a value that can be assigned to a variable, or used wherever a string can be used, for example: echo "the date is `date +%d-%m-%Y`" or mkdir backup-`date +%d-%m-%Y` -- __________________________________________________________________________ Robert Paulsen robert@paulsenonline.net old -> paulsen@texas.net
On Fri, Mar 08, 2002 at 07:55:49AM -0500, Landy Roman wrote:
See "man date" for future reference.
how do you know he already did not look at man date
I don't. I also don't know if he knows that the date command exists, or how to use back-ticks, or how to assign to a variable. But if he knew everything he wouldn't have asked. -- __________________________________________________________________________ Robert Paulsen robert@paulsenonline.net old -> paulsen@texas.net
On Fri, 2002-03-08 at 07:55, Landy Roman wrote:
See "man date" for future reference.
how do you know he already did not look at man date
I agree. That has always been the single most infuriating thing -- for me -- about learning Linux. How do you know what it is you have to do, or even where to start looking, when you are attempting something you have not done before? Most of us have met "date" quite early in our exploration. I'd bet that the fellow already knew about "date", and probably had visited the man page more than once. The actual information that he needed was the bit about using the back-ticks to capture the returned value in a variable. I looked. That was not in the "date" man page. Even if there WAS a man page about "`" (and there is not, of course), a person would have to already know that THAT was what he was looking for.... And, if he'd asked for "man variable", he would have gotten nothing useful for his situation. I know. I looked. :-) -- Kevin McLauchlan Chrysalis-ITS, Inc. "Ultimate Trust(TM)"
On Fri, Mar 08, 2002 at 09:59:07AM -0500, Kevin McLauchlan wrote:
The actual information that he needed was the bit about using the back-ticks to capture the returned value in a variable. I looked. That was not in the "date" man page.
Even if there WAS a man page about "`" (and there is not, of course), a person would have to already know that THAT was what he was looking for....
This is described in the bash man page. It's called "Command Substitution". There is lots of good info in there about shell programming, but it is not meant as a tutorial so it's a hard way to learn. -- __________________________________________________________________________ Robert Paulsen robert@paulsenonline.net old -> paulsen@texas.net
On Fri, 2002-03-08 at 10:24, Robert C. Paulsen Jr. wrote:
This is described in the bash man page. It's called "Command Substitution". There is lots of good info in there about shell programming, but it is not meant as a tutorial so it's a hard way to learn.
Speaking of which.... the other day I think I saw one of those O-Reilly "animal" books on BASH Programming. Would that be worth picking up? Or does anybody know of something better? "Better" in this case would be the best tutorial, with some handy reference material all in one book. -- Kevin McLauchlan Chrysalis-ITS, Inc. "Ultimate Trust(TM)"
On Sat, 9 Mar 2002 02:42, Kevin McLauchlan wrote:
On Fri, 2002-03-08 at 10:24, Robert C. Paulsen Jr. wrote:
This is described in the bash man page. It's called "Command Substitution". There is lots of good info in there about shell programming, but it is not meant as a tutorial so it's a hard way to learn.
Speaking of which.... the other day I think I saw one of those O-Reilly "animal" books on BASH Programming.
Would that be worth picking up? Or does anybody know of something better? "Better" in this case would be the best tutorial, with some handy reference material all in one book.
Hi Kevin, Have a look at the Advanced Bash-Scripting Guide http://www.linuxdoc.org/guides.html This document is both a tutorial and a reference on shell scripting with Bash. It assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction. The exercises and heavily-commented examples invite active reader participation. Regards Graham Smith
i feel as if I should add to this. I knew of the date cmd but when i hit a
brick wall I seem to to forget the obvious options like "man date"
Thanks for all the help
Cheers
Ray Booysen
----- Original Message -----
From: "Kevin McLauchlan"
On Fri, 2002-03-08 at 07:55, Landy Roman wrote:
See "man date" for future reference.
how do you know he already did not look at man date
I agree. That has always been the single most infuriating thing -- for me -- about learning Linux.
How do you know what it is you have to do, or even where to start looking, when you are attempting something you have not done before?
Most of us have met "date" quite early in our exploration.
I'd bet that the fellow already knew about "date", and probably had visited the man page more than once.
The actual information that he needed was the bit about using the back-ticks to capture the returned value in a variable. I looked. That was not in the "date" man page.
Even if there WAS a man page about "`" (and there is not, of course), a person would have to already know that THAT was what he was looking for....
And, if he'd asked for "man variable", he would have gotten nothing useful for his situation. I know. I looked. :-)
-- Kevin McLauchlan Chrysalis-ITS, Inc. "Ultimate Trust(TM)"
On Fri, Mar 08, 2002 at 02:12:40PM +0200, Ray Booysen wrote:
Hi, I need to get the date in the above format into a variable so my backup script can create a directory to store the archive in. any suggestions?
Thanks Ray
Try something like this: VARIABLE=`date +%d-%m-%Y` Regards, Keith -- LPIC-2, MCSE, N+ wielder of vi(m), an ancient, dangerous and powerful magic Don't get lost, show no fear, and you'll be ready for a new frontier -- d.w.
participants (7)
-
Anders Johansson
-
Graham Smith
-
Keith Winston
-
Kevin McLauchlan
-
Landy Roman
-
Ray Booysen
-
Robert C. Paulsen Jr.