ksh scripting ... get previous date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh scripting ... get previous date
# 1  
Old 02-13-2009
Question ksh scripting ... get previous date

hi all,
i am developing a korn shell script. One of the requirement is to generate the date, which is 7 days before the current date, and create a folder with that date.

So for ex: if the time command returns 13 Feb 2009, I should be able to generate 6 Feb 2009, and assign that to a variable.

my OS is AIX

can someone post or help me with some working code.

All help is greatly appreciated. I am working under a very tight deadline.

thanks
# 2  
Old 02-13-2009
Hammer & Screwdriver Not quite, but close

Code:
> touch file170
> currd=`stat -c %Y file170`
> change=60*60*24*7
> lastd=`echo "$currd - $change" | bc`
> echo $(date '+%m %d %Y' -d @$lastd)
02 06 2009

Just need to think of the formatting to have the month as Feb.
# 3  
Old 02-13-2009
Hammer & Screwdriver Here is the rest...

Code:
> echo $(date '+%b %d %Y' -d @$lastd)
Feb 06 2009
> myvar=`echo $(date '+%b %d %Y' -d @$lastd)`
> echo $myvar
Feb 06 2009

# 4  
Old 02-13-2009
this is what i get when i run the code.
maybe stat is not recognized on my system
any suggestions

nis3.scr[4]: stat: not found
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
# 5  
Old 02-13-2009
If you have perl:
Code:
days_ago()
{
  perl -e  '
	# take 86400 * # of days from right now in epoch seconds
		 $yestertime = time - (86400 * $ARGV[0]);
		 $month = (localtime $yestertime)[4] + 1;
	# day of the month
		 $day = (localtime $yestertime)[3];
	# year
		 $year = (localtime $yestertime)[5] + 1900;
	# US format mm dd yyyy
		 printf( "%02d %02d %4d\n", $month, $day, $year);  '  $1
}

#usage
# yesterday
days_ago 1
printf "last week  was %s\n" "$( days_ago 7 )"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to get previous date for the given input date..

hi all, need a script or command to get the previous date for the given input date... like in my script i will pass date as input parameter like 2014-12-01 and i want the output as previous date.. ie.. 2014-11-30 (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Date - incorrect results for previous date

Hello: I am bit puzzled with what I could be doing wrong and any help is appreciated. I have a date in YYYMMDD format and I need to find the previous date. Based on the input on this forum, I have come up with the following. It seems to work for all except the following. Here I am passing date... (3 Replies)
Discussion started by: wincrazy
3 Replies

3. Shell Programming and Scripting

How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi, i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and... (5 Replies)
Discussion started by: karthikram
5 Replies

4. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. UNIX for Dummies Questions & Answers

KSH Checking Previous Date**

How would I go about getting the previous date on the server? I'm out of ideas...I am thinking that I could do it using the date command, but I am unable to find any information on doing it. For example if the current date is April 17th 2008 it would be (20080417) <- (YYYYMMDD). I need the previous... (4 Replies)
Discussion started by: developncode
4 Replies

10. Shell Programming and Scripting

Specify a previous date as start date in shell script

Hi, I am trying to modify a script which accepts date in format dd/mm/yy. I am trying to modify the script so that it retrieves the date that was 15 days earlier from today as start date. Eg.if today is 05/09/2006, the script should retrieve 21/08/2006 as start date. Is there any script/code to... (2 Replies)
Discussion started by: ritzwan0
2 Replies
Login or Register to Ask a Question