Date Script problem for year cross over


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date Script problem for year cross over
# 1  
Old 03-02-2010
Question Date Script problem for year cross over

Hello,

I'm very new to script writing - everything I have I got off the Internet. I'm pretty sure I stole this date script from this site.

Anyway, the script works great until I try to obtain a date that falls into last year. I can get 'Dec 31, 2009' but nothing earlier. Below is the script and the error I'm getting.

How can I either fix this script or create a new one that will give me date that is X days earlier. Really, what I want is a script that will provide me a date that is 120 days earlier then the system date, but I can't figure that out so I settled for this.

Any advice you can offer is greatly appreciated.
Thank you! Leslie

Code:
ucasd80:iclac:/home/iclac/script_writing# pg move_date4
#!/bin/ksh
# This script will display the last date of the month for 2 months earlier.  Ex: if today if 6/1/2009 it will show 4/30/2009
printf "%d %d" $(date "+%Y %m") | read year month
let month=$month-4
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' | read day
printf "%02d/%02d/%02d\n" $month $day $year > 4monthsago.txt

ERROR:
Code:
ucasd80:iclac:/home/iclac/script_writing# ./move_date4
cal: Not a recognized flag: 1
Usage: cal [[Month] Year]
        Displays a calendar.


Last edited by Scott; 03-02-2010 at 01:53 PM.. Reason: Code tags please...
# 2  
Old 03-02-2010
There are several posts here on this forum for date

Search for date calculations. The following is one to find a date 7 days prior:
https://www.unix.com/unix-dummies-que...-date-7-a.html
# 3  
Old 03-03-2010
In your code you need to analyze the following things,

Code:
- If the month is 3 then "let month=$month-4" statement returns negative value. 
So "cal <negative_no> <year> will not work.

-  Piping the output to some variable is not possible in bash.
 You can change this line 
"cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' ` | read day" 
to "day=`cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' `"

- You need to change the first line also.
printf "%d %d" $(date "+%Y %m") | read year month

If piping the output to some variable is possible in your environment then use as like before.

# 4  
Old 03-03-2010
Its a simple way to solve your problem.

You have to add one more thing in your script.

If count of the month is less than 0,then add it with 12.After that,you can get the result what you want.

I have done the above changes in your script.

You try the following script.
Code:
#!/bin/ksh
# This script will display the last date of the month for 2 months earlier.  Ex: if today if 6/1/2009 it will show 4/30/2009
#printf "%d %d" $(date "+%Y %m") | read year month
year=`date +%Y`
month=`date +%m`
let month=$month-4
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
if [[ $month -le 0 ]] ; then
   let year=$year-1
   let month=12+$month
fi
echo $month
day=`cal $month $year | tr -s '\n' ' ' | awk '{print $NF}'`
printf "%02d/%02d/%02d\n" $month $day $year > 4monthsago.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

3. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

4. UNIX for Dummies Questions & Answers

How to get year part from file created date?

Hi Gurus, I need to get year part of file created date. when using ls -l , it only show month, day and time. is there any option I can add to get year portition? Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Previous Year Date

Hi Gurus, I would like to get the date for the previous year based on the date I specify. For e.g. If I input today's date (i.e. 20130422) I need to get 20120422. We don't have GNU and use K Shell. Any help is highly appreciated. Thanks Shash (4 Replies)
Discussion started by: shash
4 Replies

6. Shell Programming and Scripting

get date for all saturdays in given year

Hello Guys I browsed through site for mentioned requirement all solutions I got in perl I am looking for something in unix script for same any suggestions thanks (1 Reply)
Discussion started by: joshiamit
1 Replies

7. Shell Programming and Scripting

Listing files that belong to a certain year date?

I'm trying to list files, first by size and I'm using something like this ls -l|awk '{print $5,$6,$7,$8,$9|"sort -nr"}'|more Now I'd like to just do the same listing but only for files with the year 2009 in the $8 field or even anything less than 2011. (5 Replies)
Discussion started by: NycUnxer
5 Replies

8. Shell Programming and Scripting

Derive the Year value from the date value

Hi All, I have two dates: PREVIOUS_DAY and CURRENT_DAY. I need to test the these two values years are same or not. PREVIOUS_DAY like '%y%m%d' i.e values like 111010, 111011, 111012 etc. For CURRENT_YEAR:'date +%Y' use this command. How can derive the year value from PREVIOUS_DAY and... (1 Reply)
Discussion started by: pdathu
1 Replies

9. Shell Programming and Scripting

Help to generate date from day of the year

Hi I want to convert the day of the year(yyyyddd) to date in mmddyy format Example: input is 2005029 --------> 29th day of 2005 I have to get the output as 01292005 ---> jan 29th 2005 I've to do this in K-Shell There were threads that dealt with coverting date to day of the year but I... (3 Replies)
Discussion started by: sudhamayeev
3 Replies

10. Shell Programming and Scripting

Add 1 year to System date in script

Hi All, I wanted to add 1 year to the system date in my script. say export start_date=`date +%F` echo $start_date o/p of this is 2009-09-02 To this i want to add 1 year. the output i need here is 2010-09-02 can anybody help me ? Thanks in advance, Vinay (4 Replies)
Discussion started by: vinayakatj56
4 Replies
Login or Register to Ask a Question