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 09-24-2013, 03:57 PM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
Generating a filename from the system date


set -- `date`
year=`expr $6 : '..\(..\)'`
echo "$2$3_$year"


can someone please explain this set of commands ?
 
Old 09-24-2013, 04:10 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
just seems an incredibly tedious way of doing
Code:
date +%d%b_%y
 
1 members found this post helpful.
Old 09-24-2013, 05:47 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by Firerat View Post
just seems an incredibly tedious way of doing
Code:
date +%d%b_%y
Should be
Code:
date +%b%d_%y
At least on my machine...but either way, yes, it looks like an inefficient, fragile, tedious approach when a single date command could do it better.

To answer the fundamental question - commands in a BASH script are no different than commands on the command line, so why not just run them and see what they do? You'll learn much more than asking us to interpret code. For example, anything in back ticks is a command, and the output of which is stuck in its place.

So for the first command, "set -- `date`". First "date" is run, and the output is passed to "set --". So to test it, just run "date" on the command line, look at what the output is. Then run "set -- `date`". The next few lines are referencing $2, $3, and $6, clearly these are variables set up by the set command, so just print them out and see what they are. Now that you know what $6 is, run the expr command to see what it's doing and what it's assigning to the "year" variable. Finally, print out the final line to see all of it:

Code:
$ date
Tue Sep 24 15:52:53 MDT 2013
$ set -- `date`
$ echo $2
Sep
$ echo $3
24
$ echo $6
2013
$ expr $6 : '..\(..\)'
13
$ year=`expr $6 : '..\(..\)'`
$ echo $year
13
$ echo $2$3_$year
Sep24_13

Last edited by suicidaleggroll; 09-24-2013 at 05:54 PM.
 
2 members found this post helpful.
Old 09-24-2013, 10:29 PM   #4
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mageia, and whatever VMs I happen to be playing with
Posts: 11,683
Blog Entries: 12

Rep: Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902Reputation: 2902
A long time ago, in my first (and so far only) significant foray into BASH scripting (to automate mysql dumps in this case), I set a directory name using a date.

I'm sure this is very clumsy, but it worked. I extended it to use hours and minutes because, in testing the script, I wanted to be able to run it multiple times in the same day.

Code:
#Define now as today's day-month-year-hour-minute.

NOW=$(date +%m-%d-%Y-%H-%M)

#Make the directory to hold the backup file.

mkdir /root/backups/sql/$NOW
 
Old 09-25-2013, 02:26 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
ahh, yes my bad...
in my defence I was resisting YYYY-MM-DD , which in my opinion is the true way to represent dates

Code:
date +%Y-%m-%d

Last edited by Firerat; 09-25-2013 at 02:28 AM.
 
Old 09-25-2013, 09:35 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by Firerat View Post
ahh, yes my bad...
in my defence I was resisting YYYY-MM-DD , which in my opinion is the true way to represent dates

Code:
date +%Y-%m-%d
Agreed, that way alphabetical = chronological order.
 
  


Reply

Tags
date


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
logrotate with date in filename ptp Linux - Newbie 1 04-14-2011 11:55 AM
sarg - generating report via date - date mandrakeBren Linux - Newbie 0 10-29-2009 08:31 AM
Date in a filename. Garp Linux - General 7 12-09-2005 02:57 PM
use date as output filename? wijnands Linux - Newbie 1 05-12-2004 04:39 PM
how to add date to filename x2000koh Programming 1 07-08-2003 10:44 PM


All times are GMT -5. The time now is 03:29 AM.

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