Computing.Net > Forums > Unix > calculate previous date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

calculate previous date

Reply to Message Icon

Name: Pete
Date: May 23, 2003 at 03:16:38 Pacific
OS: Linux
CPU/Ram: PIII/256MB
Comment:

Hi, I'm new in writing unix scripts. Currently I need to write a script to calculate previous date. Lucky this web site has infomation on it. I have found the following problem from this web site which will work great for me. One thing though, I'm currently working on Linux and it doesn't have ksh installed. It only has csh, bash, and bsh. What do I need to do in order for me to convert this ksh program to bash program? Your help will be appreciated. Thank you.

#!/bin/ksh

set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
NDAY=`date +%u`
WEEKDAY=${DAYS[`date +%u`]}

if [ $YESTERDAY -eq "0" ]
then
MONTH=$((MONTH-1))

if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi

set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi

TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
# echo $WEEKDAY $YESTERDAY $MONTH $TMONTH $YEAR
echo $YESTERDAY

Pete



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 23, 2003 at 06:05:44 Pacific
Reply:

If you would like to install ksh on your linux box, look for the package pdksh.

Try running this script under bash. They have a lot of similarities. It may work.


0

Response Number 2
Name: nails
Date: May 24, 2003 at 11:38:56 Pacific
Reply:

Hi:

On my Red Hat 7.1 system:

1) obviously, change #!/bin/ksh to #!/bin/bash

2) Remove the -A option from the two set commands. bash doesn't support the -A.

Other than that, it worked.

Regards,


Nails


0

Response Number 3
Name: Pete
Date: May 25, 2003 at 23:57:11 Pacific
Reply:

Oh thank you all for helping me.


0

Response Number 4
Name: Pete
Date: May 29, 2003 at 18:51:54 Pacific
Reply:

The above sript worked great for me,Thanks for all your help. I have one more question though regarding to the above script. What does the line "shift $(($# - 1))" do in the script. I know, "shift" will shift whatever in the set one to the left but what about "$#" means? I try to find the reference on my unix books but couldn't find anything that mention this kind of expression or similar. Can someone tell me what this means? Thanks.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: calculate previous date

Calculate Previous Date in ksh www.computing.net/answers/unix/calculate-previous-date-in-ksh/4282.html

Calculation the date www.computing.net/answers/unix/calculation-the-date/6212.html

Date calculations www.computing.net/answers/unix/date-calculations/3166.html