Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date subtraction in Korn shell scripting

Status
Not open for further replies.

macrun95

Technical User
Dec 5, 2005
28
US
I was wondering if there is an easy way to subtract one day from the current date? I've found some old shell scripts online that convert to Julian, subtract, and then convert back to Gregorian. That seems like a lot of work and if there is an easier way to do it, that would be great. I need to copy a file from one directory to another with yesterday's date and need to search for that file and copy it. The format is IDXMS-MSY-031708T0601.RPT.

Thanks,
Eric
 
PHV's excellent faq80-4800 will help here.

On the internet no one knows you're a dog

Columb Healy
 
To elaborate from my earlier posting try
Code:
#!/bin/ksh

#GetDate function from PHV's FAQ
GetDate(){ # GetDate nDays [format]
  typeset -i nDays=$1; format=$2
  eval $(echo $TZ | sed '
s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
  TZ=$zon1$((localOffset-24*nDays))$zon2 date $format
}

#Find files in source dir with yesterday's date and move them to object dir
find /source/dir -name "IDXMS-MSY-$(GetDate 1 +%m%d%y)*" -exec mv {} /object/dir \;

On the internet no one knows you're a dog

Columb Healy
 
Thanks for the help. I will look at the datecalc script and see if it will help with what I need. The one from columb worked really well. I tested it on my test lpar with all different kinds of dates and it worked every time.

Thanks again!

[2thumbsup]

Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top