Earlier this morning I needed to whip up some time delayed action on one of my servers. This is normally something I would just drop into a crontab and kind of you know forget about. Honestly I love my crontabs but they can become rather unwieldy once you exceed a screen of entries especially if you have any sort of complex time signatures.
In addition when you want to simply delay the execute of a script for a short period of time setting up additional cron jobs can be slightly burdensome especially for these sorts of egg timer situations. You have to remember to cleanup your crontabs regularly. I am already exporting a few handy convenience environment variables for creating egg timer scripts in my .bash_login for example:
export MIN=60 export HOUR=3600 export DAY=86400
These three variables are available to all scripts that run on my system and yes they are there for the reason of lazy. However as my programming professor at NYU once stated something to the effect of, “Programmers are indeed the laziest people, because they work at making less work for themselves…” I added these constants so that I wouldn’t have to remember them. I know it sounds really silly but consider you want to know when 13 minutes had passed then you could do something crazy.
> sleep `echo "${MIN} * 13" |bc`; echo "13 minutes is up, the eggs are done!"
I don’t know about you but as helpful as this is it really is not the cleanest way yo code this. Sure you could replace the calculation with the actual value but honestly this isn’t really all that clear either.
> sleep 780; echo "13 minutes is up, the eggs are done!"
Therefore I decided that this needed to be a bit clearer. Also if you’ve followed me for a while I am not a huge fan of the whole one liner movement. While I feel that one liners like Henny Youngman do have their place, I prefer to have clear and easily interpreted code especially if it is something that is going to reside in a script that I may not visit again for years. Code should be relatively elegant and easily readable requiring little brain power to interpret it’s meaning. If you need more lines of comments than there are of code then you are likely doing something wrong.
So I needed something like the egg timer above so I wrote one into my login script. Which turned out to relatively easy once I took a step back and thought about it. For backward compatibility reasons I decided to build upon the constants I had already defined.
# hours - returns X hours as seconds.
function hours() {
hour=3600
if [[ "${1}" ]];
then
echo "${hour} * ${1}" | bc
else
echo ${hour}
fi
}
The advantage of using a function is the ability to pass in parameters. Also it is worth noting that bash does not really support the concept of constants so ultimately a function will be safer in the long run. What I mean is that you can always change the value of $HOUR but not the value of hours( ). If you’ve inserted the above into your .bash_login file and relogged in then you should be able to play along.
> hours 3
10800
> hours
3600
Since I created a function for minutes, hours and days we can now do the following;
> sleep `minutes 13`; echo "The eggs are done!"
I don’t know about you but that is pretty clear and even elegant. Obviously this may not be your cup of tea and that is ok. Besides I drink coffee anyway. So what’s your handy addition to your .bash_login? Share some insight in the comments
Related articles
Leave a Reply