If a date 20141220 as parameter to the script, then the script has to return the output as 20141219.
i.e given date - 1.
The requirement is simple. But it should satisfy leap year, the months having 31 and 30 days, the date in which day light savings changes happens, etc,.
The date has to be calculated based on the server date and time.
That depends on what your system - which you fail to mention - provides. Above is an example of GNU date, which is not avaiable on every system. (Free)BSD provides
date -v-1d -jf "%Y%m%d" 20141220 +"%Y%m%d"
20141219
Recent bash has the printf "%(datefmt)T" builtin, but that needs epoch seconds, which may be difficult to get at.
Pravin's code will work if you have GNU date in your system. Following code may help you to get the previous date of any input date passed by you to script as follows.
If you don't have GNU date, then another alternative is perl if you have that. From your input date, we need to split it up the values and feed them in:-
#!/bin/ksh
input_date=$1
in_yyyy="${input_date%????}" # Remove last four characters
in_dd="${input_date#??????}" # Remove lead four characters
in_mm="${input_date#????}" # Remove lead four characters
in_mm="${in_mm%??}" # Remove last two characters from previously split value
((temp_mm=$in_mm-1)) # Reduce month by one as epoch counts from 1/1/1970 as zero
# Get time in seconds since epoch
perl -e "use Time::Local; print timelocal(0,0,0,$in_dd,$temp_mm,$in_yyyy), ;" | read in_seconds
# Go back one day in seconds
((out_seconds=$in_seconds-86400))
# Display answer in required format
perl -e 'use POSIX qw(strftime);print scalar(strftime "%Y%m%d", localtime $ARGV[0]), "\n";' $out_seconds
I hope that this meets the brief you have given us. There may be smarter ways to read & split the input date, so I'm open to suggestions.
Obviously some validity checking on the input would be wise else you will get various horrible messages from the perl
I've put it in a script called 254222 (thread number) and tested it as shown:-