Category Archives: Shell Scripts

Date Arithmetic in Shell Scripts

I needed to be able to do date arithmetic do be able to do some stuff with log files. And I wanted to do this in shell script. Only then did I realize what a pain to deal with dates in shell scripts. After searching a bit, I found this link with various options. Many included using perl. I thought I would go with perl, but even there I didn’t exactly get what I needed (ofcourse, as perl is a full fledged programming language, it would have been lot less painful to do what I needed than in pure shell, but still).

My requirement is to start from a date and get the next 7 dates. So, the simple solution I came up with is to pre generate the dates and then use the shell script to get what I needed.

For example, “grep -A 7 ‘2006/07/04’ dates.txt” will give me the 7 dates starting from 2006/07/04, American Independence Day.

So, while the technique I am using does’t provide the real arithmetic capability, it solves my needs. And I think in a clever way. Oh, btw, I used BeanShell to pre-generate these dates.

If you are new to shell scripting, any of these shell scripting books may be useful.

Leave a comment

Filed under Shell Scripts