Get Yesterday’s Date
I was working on a script that parsed log files. The file names were log.mmddyy (log.080811). This was a little bit of a learning experience for me (new to shell scripting). I was working on a Solaris 10 box, but I thought I’d do all my dev/testing on Ubuntu. Big mistake. Linux was running bash/gnu tools. Solaris was not. After I figured out how to do it on Ubuntu, I had to figure out how to do it on Solaris (thanks Scott!), then I did it in Powershell (had done something similar already for another script) and since I’m teaching myself Python, I wanted to do it in Python.
Are they the “right” way to do them? They work! Probably not a “right” way to do them, as there is always more than one way to do something
Shell
#!/bin/bash os=`uname` if [ $os == 'Linux' ]; then yesterday="$(date -d 'yesterday' +%m%d%y)" echo $yesterday elif [ $os == 'SunOS' ]; then yesterday=`TZ=GMT+24 date +%m%d%y` echo $yesterday fi
Gnu Date
#!/usr/bin/bash ## Get Yesterday yesterday="$(date -d 'yesterday' +%m%d%y)"
Powershell
$yesterday = (get-date -format mmddyy).adddays(-1) $yesterday.tostring('MMddyy')
Python
import datetime today = datetime.datetime.today() one_day = datetime.timedelta(days=1) yesterday = today - one_day yesterday.strftime('%m%d%y')
Tags: bash, date, linux, powershell, python, shell, unix, yesterday
