POST DIRECTORY
Category software development

As developers, we write programs to improve the efficiency of our clients and their customers. That’s pretty well the whole goal of the software industry.

So how about putting those mad software skills to work for yourself? Clever programmers are always on the lookout for easier ways to perform repetitive tasks and find creative uses for the tools they already have. Writing small, innovative scripts can save incremental amounts of time on frequently performed tasks. Over time, these small savings will add up to enormous improvements in efficiency.

The key to writing useful scripts is to pay attention to your workflow. It’s personal, so do what works for you! The following examples illustrate some diverse instances where scripting has saved me time and streamlined my programming landscape. While these exact examples may not fit your needs, they should serve to demonstrate how even simple scripts can enhance your productivity.

Example 1. Find it Faster

I often use similar variations of the find command to locate files. The base command looks like this:

find PATH -iname '*SEARCH_TERM*'

I took a few minutes to write a small wrapper function to make it easier to type and to use.

# fname.bash
function fname {
  if [[ $# -gt 1 ]]; then
    path_name="$1"
    name="$2"
  else
    path_name="."
    name="$1"
  fi

  find $path_name -iname "*$name*"
}

Now, I can simply run fname [PATH] SEARCH_TERM to perform my searches. These small improvements can be made anywhere the same process is used over and over again and will generate a surprising boost in efficiency.

Example 2. Deploy without Delay

At HCW we use a common pattern for our Heroku server environments. We have an integration environment for testing, a demo environment for demos, and a production environment for the live site. This pattern works great for our deployment flow, but it makes running heroku commands cumbersome because you must specify the environment’s app-name in every single command.

To make these commands easier, I wrote this small script heroku.bash. Now, after setting up the git remotes, I can simply run a command on the production environment like this:

hprod COMMAND
# Versus
heroku COMMAND --app LONG_AND_DIFFICULT_PRODUCTION_APP_NAME

Or on the integration environment with:

hint COMMAND
# Versus
heroku COMMAND --app LONG_AND_DIFFICULT_INTEGRATION_APP_NAME

This works on all of our heroku projects. It saves me time and eliminates the necessity to remember (or lookup) each environment’s Heroku app name.

Example 3. Pingme!

This simple notification script is one of the most powerful tools in my efficiency arsenal:

#!/bin/bash
# ~/bin/pingme # file permissions -> 755

# Adjust this to point to a nice notification sound
FULL_PATH_TO_AUDIO_FILE="$HOME/misc/sounds/notification.wav"

function play_sound {
  # Adjust this command as needed for your operating system
  afplay $FULL_PATH_TO_AUDIO_FILE
}

if [[ $# -gt 0 ]]; then
  # Assume a PID has been passed

  while [[ $(ps -e | grep $1 | wc -l) != "0" ]]; do
  sleep 1
  done

  play_sound
else
  play_sound
fi

In its simplest form, the script plays a short sound. Yet, when used in conjunction with other commands, this becomes incredibly useful. Take this command for instance:

rspec; pingme

Now, as soon as your specs are done, a sound will notify you.

By simply adding ; pingme to the end of any shell command you can alleviate the necessity of monitoring most long running tasks. The script above can also be used to monitor a task that is already running by passing a PID to the command.

# Play the notification sound when the process with PID 1123 ends
pingme 1123

As your library of scripts grows over time, you’ll find that your work gets easier and easier, and that your productivity will continue to climb. Keep in mind that your scripts don’t have to be perfect. Write something that works, then improve them as your requirements change or you find new and better ways to accomplish your tasks.

''