LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 02: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, 03:10 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

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

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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 04:54 PM.
 
2 members found this post helpful.
Old 09-24-2013, 09:29 PM   #4
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,382
Blog Entries: 28

Rep: Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164Reputation: 6164
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, 01:26 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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 01:28 AM.
 
Old 09-25-2013, 08:35 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:33 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
Open Source Consulting | Domain Registration