LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-29-2011, 06:53 AM   #1
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Rep: Reputation: Disabled
Date as parameter


Hello there,

Can anybody tell me how can I retieve data for specific number of days.
For eg: User will pass date paretemer as 20110729 to my shell script
2nd parameter will be no of days say - 7

So I need to retrieve data from 20110929 to last seven days i.e from 20110923 to 20110929
 
Old 09-29-2011, 07:14 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try the -d option of the date command to specify a date different from today. You can also add or subtracts days from a reference date at your pleasure, e.g.
Code:
#!/bin/bash
for i in $(seq -$(($2-1)) 0)
do
  date -d "$1 $i days" +%Y%m%d
done
 
Old 09-29-2011, 07:32 AM   #3
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks
I tried as suggested by you but getting below error
for i in $(seq -$(($2-1)) 0)
do
sq_date=$(date -d "$1 $i days" +%Y%m%d)
echo 'sq_date' $sq_date >>$LOGFILE 2>&1
done


Error as below
date: invalid date `20110929 -3 days'
date: invalid date `20110929 -2 days'
date: invalid date `20110929 -1 days'
 
Old 09-29-2011, 07:38 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What OS is running on this system? Does the date command accept the -d option?
 
Old 09-29-2011, 11:58 PM   #5
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Its Linux. Yes -d command does work.
I have already used it in my script as below and its working absolutely fine.
SYSDAY=$(date -d yesterday +"%Y%m%d")
 
Old 09-30-2011, 12:58 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
That's strange. Please try this instead (without negative numbers):
Code:
for i in $(seq $(($2-1)) -1 0)
do
  sq_date=$(date -d "$1 $i days ago" +%Y%m%d)
  echo 'sq_date' $sq_date >>$LOGFILE 2>&1
done
On my system:
Code:
$ ./test.sh 20110929 3                                                                                            
sq_date 20110927
sq_date 20110928
sq_date 20110929
If it still doesn't work, please post the output from:
Code:
bash -x yourscript.sh
to see debugging messages from the shell. And please, use CODE tags to embed the code: CODE tags can be put in advanced mode by selecting the text to embed and clicking the # button. Or you can explicitly write them around the text as
[CODE]code here[/CODE]
Thank you.
 
1 members found this post helpful.
Old 09-30-2011, 01:46 AM   #7
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
thanks dear
this one working absolutely fine

sq_date 20110928
sq_date 20110929
sq_date 20110930
 
Old 09-30-2011, 03:35 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome! I suspected that negative numbers has been introduced in a recent version of date. Actually, I have the habit to use the "n days ago" syntax.
 
Old 10-11-2011, 05:06 AM   #9
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
this time my date format is MMDD and need similar code as above

for i in $(seq $(($2-1)) -1 0)
do
sq_date=$(date -d "$1 $i days ago" +%m%d)
echo 'sq_date' $sq_date >>$LOGFILE 2>&1
done

Parm1: 0804 (date in MMDD format)
Parm2: 2 (No of days)
expected result is :
sq_date 0803
sq_date 0804

can you tell me where am I going wrong
I am getting below output which is wrong
1010
1011

Its picking current date by default I guess

Last edited by barkha_st; 10-11-2011 at 06:14 AM.
 
Old 10-11-2011, 06:26 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
  sq_date=$(date -d "$1 $i days ago" +%m%d)
$1 here is the first positional parameters, that is the argument you pass to the script. If you run the script without arguments, $1 is a null string and the resulting date command is:
Code:
  sq_date=$(date -d "$i days ago" +%m%d)
that is it subtracts $i days from today. In your first post you stated "User will pass date paretemer as 20110729 to my shell script". If this condition is not true anymore you have to change $1 with something else, depending on how the date is defined.
 
Old 10-11-2011, 06:38 AM   #11
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Yes I will be passing 1st date parm which will be in MMDD format
and as earlier my 2nd Parm is no of days for which data should be retrieved

So if
1st Parm 0907
2nd Parm 2

So I need to retrieve data for dates 0906 and 0907 i.e for 2 days from date 0907
Its similar to my 1st script only difference here is I have date format in MMDD format instead of YYYYMMDD
 
Old 10-11-2011, 06:45 AM   #12
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
sorry ......my script is working fine
I had wrongly defined parameters
all OK now.....thanks for your help
 
Old 10-11-2011, 06:46 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Ok. Then the problem is that the MMDD format is interpreted by the date command as HHMM, so it return the current date at 09:07 hours. If you want to retrieve month and day you need to add the year manually, e.g.
Code:
sq_date=$(date -d "2011$1 $i days ago" +%m%d)
The only problem is when the date is the end of February and you cannot distinguish between leap and non-leap years. I think it's better to stick to a yyyymmdd format to take in accounts leap years calculations.
 
1 members found this post helpful.
Old 10-11-2011, 06:54 AM   #14
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
hey thanks heaps
your suggestions are really very helpful.....will consider YYYYMMDD format only
 
  


Reply


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
need to echo / printf string containing date as parameter in cron v8625 Linux - Newbie 1 02-25-2011 06:31 PM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Modify default date parameter in Search J.W. LQ Suggestions & Feedback 2 11-08-2004 12:03 PM

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

All times are GMT -5. The time now is 03:02 PM.

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