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

January 23, 2011

Shell Script: Adding days to a date input

(image taken from smashingapps.com)

Last week, I spent approximately 2 hours trying to figure out how I could add 1 day to a shell script's date input.
And, this is the solution I came up with:










Here is the code:
#--- pretend this is your date input in YYYYMMDD format (string)
datestring='20110123'

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

#--- get the formatted date input and add one day to it
dateplusone=$((datestamp +1))

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 "Finally after 2 hours you nailed it! Your log file is:" $logfile "\nTime to grab a cup of coffee and move on to the next task!"

touch $logfile


Hope this helps someone with the same problem :)

January 1, 2011

Hello World!

It's the famous programmer's test print code of all time.

It stands true to its meaning...that and being the first post in this blog.

And so at the brink of 2011...