Posts Tagged ‘bash’

Get Yesterday’s Date

Tuesday, August 16th, 2011

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')

One cool Bashism

Thursday, September 9th, 2010

Sometimes when working the cli you need to quickly run the last command, and either:

1. change something or 2. remove something

Using ^ (caret) substitution makes that easy, it runs the previous command substituting or removing what you specify by the carets (^^) here are couple of real *examples that I use frequently (and I’m sure you can think of quite a few more):

1. Change something.

Since I work on the cli a lot, and jump from host to host to get information, do stuff, get files, etc. I myself will sometimes jump to the wrong host, and as long as they are part of the same domain you can easily rerun the ssh command without having to type it all out again, e.g.:

shell#> ssh host1.mydomain.com now if host2 lives on same domain you can CTRL+C then shell#> ^host1^host2 and the shell will run ssh host2.mydomain.com.

2. Remove something.

Sometimes I want to remove an [option|name|file|pipe|blah] from the previous command:

Test to see if all dependencies for package are met:

shell#> rpm -ivh --test really-long-named-rpm.with.version.number.rpm

I don’t necessarily want to hit the up arrow (or k if you’re leet enough to use vi mode in cli (set -o vi)), backspace (no I’m not going to cover all the vi commands), remove –test and re-run the command. Here’s where ^ substitution comes into play, if I run:

shell#> ^--test^

It will happily re-run the previous command sans the –test option (you have to be specific, if you just did ^test^ it would leave the — (double dash) and might cause havoc, depending on command). So the substitution works from left to right, and only on the first occurrence. It works like this ^thing_to_replace^replacement. So in the above example we are substituting nothing for –test, essentially removing it. Obviously the one major caveat is that it only works on the previous command in the history.

Share and enjoy.

*Yes these examples may be lame, but they give you an idea.

Meta:
Contact:
Scott:
Systems Administrator, Geek, Horror fan.

Ways to reach Scott:
Twitter
FriendFeed
Facebook
LinkedIn
Tumblr
Zerply
About.me

Barry:
Systems Administrator / Geek.

Ways to reach Barry:
Twitter
Friendfeed
Associations:
Twitter:

View more tweets | Powered by HL Twitter