January 26, 2011

Shell Script: Adding days to a date input - Part 2


Apparently, there is still something wrong with the code in my previous post.

Here a quick solution to the problem: shift_date.sh





Code:
#!/bin/ksh

date_next=/home/micaela/date_next.txt

#--- pretend this is your date input in YYYYMMDD format (string)
datestring='20101231'

#--- convert string to date
datestamp=`date --date $datestring +%Y%m%d`

#--- get the formatted date input (this time using shift_date.sh) and add one day to it
./shift_date.sh $datestamp 1 >> $date_next
dateplusone=`grep '[0-9]' $date_next`

echo "The date string is: " $datestring
echo "Date plus one is: " $dateplusone

#--- use it as a log file time stamp
logfile=daily_log_$dateplusone.txt

echo -e "Your log file is:" $logfile

touch $logfile


Special thanks to Ate Nessel for pointing out the bug :D

No comments:

Post a Comment