5

I am new to scripting. I need a script in the AIX environment to get the next days date based on the date entered by the user.

After hard time browsing for this, I got a piece of code which does the reverse of the goal which i had to achieve. It gets me one day before (yesterday) the date entered by the user.

The code is this

#!/bin/ksh
echo "Enter the date (YYYY/MM/DD):"
read date
YEAR=`echo $date | cut -d"/" -f1`
MONTH=`echo $date | cut -d"/" -f2`
DAY=`echo $date | cut -d"/" -f3`
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
echo "Yesterday's Date is $YEAR/$MONTH/$DAY"

Can someone please help achieve to get the next day of the user entered date.

Desired Output:

Enter the date (YYYY/MM/DD): 2013/09/30

Tomorrow's Date is 2013/10/1
2

With relatively recent versions of ksh93:

$ printf "%(%Y/%m/%d)T\n" "2014/06/20 +1 day"
2014/06/21

Or:

$ printf "%(%Y/%m/%d)T\n" "2014/06/20 next day"
2014/06/21  
$ printf "%(%Y/%m/%d)T\n" "2014/06/20 tomorrow"
2014/06/21
| improve this answer | |
0

Pretty sure AIX does not have GNU date by default. Depending on your version of perl, you can do:

perl -MTime::Piece -MTime::Seconds -le '
    $tomorrow = Time::Piece->strptime($ARGV[0], "%Y/%m/%d") + ONE_DAY;
    print $tomorrow->ymd("/")
' 2013/10/28

So, in ksh

#!/bin/ksh
read date?"Enter the date (YYYY/MM/DD): "
tomorrow=$(
    perl -MTime::Piece -MTime::Seconds -le '
        $tomorrow = Time::Piece->strptime($ARGV[0], "%Y/%m/%d") + ONE_DAY;
        print $tomorrow->ymd("/")
    ' "$date"
)
echo "Tomorrow is $tomorrow"

Running this looks like:

Enter the date (YYYY/MM/DD): 2013/12/31
Tomorrow is 2014/01/01

Tested on an old version of ksh and perl

$ what /usr/bin/ksh
/usr/bin/ksh:
        Version M-11/16/88i
        SunOS 5.8 Generic 110662-24 Apr 2007
$ perl --version

This is perl, v5.10.0 built for sun4-solaris
| improve this answer | |
  • No input validation is performed here, so if the user enters an invalid date format, perl will complain. – glenn jackman Oct 28 '13 at 17:53
  • :I dont have perl modules installed in the machine I use. So is there any way to achieve this without perl. – cm60 Oct 28 '13 at 17:59
  • Did you check? I believe that Time::Piece and Time::Seconds are core Perl modules. – glenn jackman Oct 28 '13 at 18:06
  • Yes. I had tried it earlier I was getting perl modules not found errors. I do not have admin access for that machine to install the perl modules. – cm60 Oct 28 '13 at 18:35
  • :Can you please help me to edit the above piece of code. – cm60 Oct 28 '13 at 18:37
0

Using the Perl approach, with just a very basic/common modules (Time::Local and POSIX):

#!/bin/ksh
echo "Enter the date (YYYY/MM/DD):"
read date
timestamp=`perl -MTime::Local=timelocal -e '@t = split(/[-\/]/, $ARGV[0]); $t[1]--; print timelocal(1,1,1,reverse @t);' $date`
YESTERDAY=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0] -86400));' $timestamp`
TODAY=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0]));' $timestamp`
TOMORROW=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0] +86400));' $timestamp`
echo "YESTERDAY= $YESTERDAY"
echo "TODAY= $TODAY"
echo "TOMORROW= $TOMORROW"

Example:

Enter the date (YYYY/MM/DD):
2014/02/28
YESTERDAY= 2014/02/27
TODAY= 2014/02/28
TOMORROW= 2014/03/01

If don't have the Time::Local module, this can still be done with a bit more of pain. However, the POSIX module (strftime method) is mandatory as being the "smart" part of the thing. Without it, it would be difficult to go from "28-feb" to "01-mar".

| improve this answer | |
-1

You can try the following:

currDate=$(date); nextDay=$(date -d "$currDate + 1 day" +"%Y-%m-%d"); echo "$nextDay"
| improve this answer | |
-1
printf "Enter the date (YYYY/MM/DD):" ; read user_date

desired_date=`date --date="$user_date+1 day" +"%Y/%m/%d"`

echo "Tomorrow's Date is $desired_date"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.