Let Bash make the birthday wish

One of my colleagues birthday was a few days ago (December 22). He is our network/sysadmin and is a big Linux fan (nothing surprising, all sysadmins of the world tend to be Linux fan). So I decided to write a one line bash script for him and posted it to his Facebook wall.

[ "`date | cut -d ' ' -f 2,3`" == "Dec 22" ] && echo "happy birthday to LL bhai"

What it does? Nothing serious at all. If it is run on December 22 then it will print happy birthday to LL bhai (“LL bhai” is his name, nothing related to bash).

How it does its work? Pretty simple. date command is used to get the system date. Output is piped to the cut command which splits the date string by space and retrieves field 2 and 3. Then that is compared against target "Dec 22" by using [ command which is equivalent (other than the syntactic sugar) to test command. If the comparing is true then the second part of logical and (&&) is executed which is a echo command. And that’s all.

The fun part is if this single line is placed in bash startup file (~/.bash_profile if interactive login shell and ~/.bashec if interactive but not login shell) then on every year our LL bhai will get this wish. Every time bash is run on Dec 22, this wish will be printed. Everybody may forget, but Bash will never forget his birthday.

Just a few ending notes and references:

  • Bash Guide for Beginners and Shell Programming chapter on Beginning Linux Programming might be the best places to start for complete beginners to bash scripting.
  • This is not a great script. It is possible to remove the cut command entirely by using some options to date. No doubt, there are more than one ways to do a single task.
  • It requires some changes to work on other date. First, “Dec 22” is hardcoded, and second, if the date is one digit long then the argument of cut requires changing.

Happy Linuxing.

This entry was posted in Linux and tagged . Bookmark the permalink.

2 Responses to Let Bash make the birthday wish

  1. Anupom says:

    Very Clever. What a birthday gift for a Sys Admin !
    LL must be happy about it 🙂

  2. arcolin says:

    What a great idea! Just wished a friend in bash… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s