Thursday, January 24, 2008

A Unix And Linux Shell Script To Remind You Of Yesterday

For today's post, I thought we'd take a little break from all the graphing and harken back to a simpler time. Or, at least a time when my life was simpler ;)

Our script today is almost totally unnecessary in this day and age. If you use Perl's localtime function or know how to use the date command, this stuff is all taken care of for you. However, the script still stands as a good example of taking into account all the small things that go into something as simple as working with time and date variables that span the passing of a day.

Often, simple scripting constructs like these are overlooked, because they're built in to almost every Operating System and scripting language available at this point in time. However, we do use them almost every time we're tasked with automating menial administration tasks. For instance, if you want to roll the log file for any application, it's usually a good idea to do it just at midnight, at which point it's today, but you need to strip all of the values from your log file from yesterday and, for convenience, time stamp that chunked off log.

Our script today will absolutely positively work on RedHat Linux, Sun Solaris and every other flavor of Unix and/or Linux. It's about as basic as basic gets (although I did go with ksh/bash scripting to simplify the arithmetic and not confuse the issue with a whole bunch of cryptic expr calls).

This script is meant to be more of a function (or a file to be sourced into another script). I only included the bottom "echo" statements for illustrative purposes. You should remove those if you ever use this script, and I've included a legend at the bottom to show example values you can expect to get from the script's variables.

Have fun with this and check it out. If you can't avail yourself of any built-in date/time functions on your system, this might be of great use to you. Going through writing a script like this makes you appreciate the fact that you don't have to know all that much about the calendar or deal with all that messy stuff like "leap years," etc.

You can source in this file by saving it as whatever name you choose (let's say shelldate.h) and adding this line near the top of your script:

. /wherever/you/put/this/shelldate.h

Or you can use this as a function by just wrapping it in a function construct, like so:

function shelldate {
CODE HERE
}


and then using it in your script like so:

shelldate

or

shelldate()

Best Wishes - We'll get back to porting those graph scripts to Linux on tomorrow's post :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/ksh

###########################################################
# Dayback - For insertion into other #
# scripts. Takes all aspects of date #
# back one ( or more with slight #
# variation). #
###########################################################
# All variable output listed at bottom #
###########################################################
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
###########################################################

###########################################################
# Main Variables #
###########################################################

SHORTALPHADAY=`date +%a`
LONGALPHADAY=`date +%A`
NUMBERDAY=`date +%d`
SHORTALPHAMONTH=`date +%b`
LONGALPHAMONTH=`date +%B`
NUMBERMONTH=`date +%m`
SHORTYEAR=`date +%y`
LONGYEAR=`date +%Y`
set -A SHORTALPHADAYS Mon Tue Wed Thu Fri Sat Sun
set -A LONGALPHADAYS Monday Tuesday Wednesday Thursday Friday Saturday Sunday
set -A SHORTALPHAMONTHS Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
set -A LONGALPHAMONTHS January February March April May June July August September October November December

###########################################################
# The Number Section #
###########################################################

###########################################################
# Numbered Days #
###########################################################

if [ $NUMBERDAY -eq 01 ]
then
if [ $SHORTALPHAMONTH == "Feb" -o "Apr" -o "Jun" -o "Aug" -o "Sep" -o "Nov" ]
then
YESTERNUMBERDAY=31
elif [ $SHORTALPHAMONTH == "Jan" -o "May" -o "Jul" -o "Oct" -o "Dec" ]
then
YESTERNUMBERDAY=30
elif [ $SHORTALPHAMONTH == "Mar" ]
then

# Check for leap year

let LEAPYEAR=${LONGYEAR}%4
if [ $LEAPYEAR -eq 0 ]
then
YESTERNUMBERDAY=29
else
YESTERNUMBERDAY=28
fi
fi
else
let YESTERNUMBERDAY=$NUMBERDAY-1
COUNT=`echo "$YESTERNUMBERDAY" |wc -m`
if [ $COUNT -eq 2 ]
then
YESTERNUMBERDAY=0${YESTERNUMBERDAY}
fi
fi

###########################################################
# Numbered Months #
###########################################################

if [ $NUMBERDAY -eq 01 ]
then
if [ $NUMBERMONTH -eq 01 ]
then
YESTERNUMBERMONTH=12
else
let YESTERNUMBERMONTH=$NUMBERMONTH-1
COUNT=`echo "$YESTERNUMBERMONTH" |wc -m`
if [ $COUNT -eq 2 ]
then
YESTERNUMBERMONTH=0${YESTERNUMBERMONTH}
fi
fi
else
YESTERNUMBERMONTH=$NUMBERMONTH
fi

###########################################################
# Numbered Years #
###########################################################

###########################################################
# Short Years #
###########################################################

if [ $NUMBERMONTH -eq 01 -a $NUMBERDAY -eq 01 ]
then
if [ $SHORTYEAR -eq 00 ]
then
let YESTERSHORTYEAR=99
else
let YESTERSHORTYEAR=$SHORTYEAR-1
COUNT=`echo "$YESTERSHORTYEAR" |wc -m`
if [ $COUNT -eq 2 ]
then
YESTERSHORTYEAR=0${YESTERSHORTYEAR}
fi
fi
else
YESTERSHORTYEAR=$SHORTYEAR
fi

###########################################################
# Long Years #
###########################################################

if [ $NUMBERMONTH -eq 01 -a $NUMBERDAY -eq 01 ]
then
let YESTERLONGYEAR=$LONGYEAR-1
else
YESTERLONGYEAR=$LONGYEAR
fi

###########################################################
# The Alpha Section #
###########################################################

###########################################################
# Alpha Days #
###########################################################

###########################################################
# Short Days #
###########################################################

i=0
while [ $i -ne 7 ]
do
if [ $SHORTALPHADAY == ${SHORTALPHADAYS[$i]} ]
then
if [ $SHORTALPHADAY == "Mon" ]
then
YESTERSHORTALPHADAY="Sun"
break
fi
let j=$i-1
YESTERSHORTALPHADAY=${SHORTALPHADAYS[$j]}
break
else
let i=$i+1
fi
done

###########################################################
# Long Days #
###########################################################

i=0
while [ $i -ne 7 ]
do
if [ $LONGALPHADAY == ${LONGALPHADAYS[$i]} ]
then
if [ $LONGALPHADAY == "Monday" ]
then
YESTERLONGALPHADAY="Sunday"
break
fi
let j=$i-1
YESTERLONGALPHADAY=${LONGALPHADAYS[$j]}
break
else
let i=$i+1
fi
done

###########################################################
# Alpha Months #
###########################################################

###########################################################
# Short Months #
###########################################################

if [ $NUMBERDAY -eq 01 ]
then
i=0
while [ $i -ne 12 ]
do
if [ $SHORTALPHAMONTH == ${SHORTALPHAMONTHS[$i]} ]
then
if [ $SHORTALPHAMONTH == "Jan" ]
then
YESTERSHORTALPHAMONTH="Dec"
break
fi
let j=$i-1
YESTERSHORTALPHAMONTH=${SHORTALPHAMONTHS[$j]}
break
else
let i=$i+1
fi
done
else
YESTERSHORTALPHAMONTH=$SHORTALPHAMONTH
fi

###########################################################
# Long Months #
###########################################################

if [ $NUMBERDAY -eq 01 ]
then
i=0
while [ $i -ne 12 ]
do
if [ $LONGALPHAMONTH == ${LONGALPHAMONTHS[$i]} ]
then
if [ $LONGALPHAMONTH == "January" ]
then
YESTERLONGALPHAMONTH="December"
break
fi
let j=$i-1
YESTERLONGALPHAMONTH=${LONGALPHAMONTHS[$j]}
break
else
let i=$i+1
fi
done
else
YESTERLONGALPHAMONTH=$LONGALPHAMONTH
fi

###########################################################
# Todays Variables: (Example) #
###########################################################
# SHORTALPHADAY = Thu #
# LONGALPHADAY = Thursday #
# NUMBERDAY = 01 #
# SHORTALPHAMONTH = Jan #
# LONGALPHAMONTH = January #
# NUMBERMONTH = 01 #
# SHORTYEAR = 98 #
# LONGYEAR = 1998 #
###########################################################
# Yesterdays Variables: (Example) #
###########################################################
# YESTERSHORTALPHADAY = Wed #
# YESTERLONGALPHADAY = Wednesday #
# YESTERNUMBERDAY = 31 #
# YESTERSHORTALPHAMONTH = Dec #
# YESTERLONGALPHAMONTH = December #
# YESTERNUMBERMONTH = 12 #
# YESTERSHORTYEAR = 97 #
# YESTERLONGYEAR = 1997 #
###########################################################

echo SHORTALPHADAY = $SHORTALPHADAY
echo LONGALPHADAY = $LONGALPHADAY
echo NUMBERDAY = $NUMBERDAY
echo SHORTALPHAMONTH = $SHORTALPHAMONTH
echo LONGALPHAMONTH = $LONGALPHAMONTH
echo NUMBERMONTH = $NUMBERMONTH
echo SHORTYEAR = $SHORTYEAR
echo LONGYEAR = $LONGYEAR
echo YESTERSHORTALPHADAY = $YESTERSHORTALPHADAY
echo YESTERLONGALPHADAY = $YESTERLONGALPHADAY
echo YESTERNUMBERDAY = $YESTERNUMBERDAY
echo YESTERSHORTALPHAMONTH = $YESTERSHORTALPHAMONTH
echo YESTERLONGALPHAMONTH = $YESTERLONGALPHAMONTH
echo YESTERNUMBERMONTH = $YESTERNUMBERMONTH
echo YESTERSHORTYEAR = $YESTERSHORTYEAR
echo YESTERLONGYEAR = $YESTERLONGYEAR


, Mike