shstack: Persistent and Easy to Use Stacks Shared Between Shell Sessions

| Comments

This little Perl program has been sitting in my ~/bin directory for almost a year now. It has quite a few features that I don’t use very often, and some of those rarely used features seem to have a lot of bugs. Even so, I figured that it was time to throw it up on GitHub.

Where did shstack come from?

Fairly often, I get into a situation where I am performing different parts of the same task in two or more terminal windows. A lot of times I wish I had easier access to path and file names in the other window. I thought I’d write up a little script to automate the process.

I very quickly realized that it would be handy to be able to work with more than one file at a time, so emulating pushd and popd made a lot of sense. Since I was already planning to store the stacks in a file, I decided that they might as well be persistent.

What can you do with shtack?

I store the DNS names of some important servers in a stack called servers. I can use shstack’s built-in for command to ping them all to see if they are alive.

Why reinvent the for loop?

I have two reasons. The first is shstack’s “safe” for loop (sfor). The sfor loop works like a regular for loop, but it removes each item from the stack when the command is completed successfully. You might use this if you wanted to rsync a directory to multiple servers. That way, if there are any errors, you can easily fix the problem and run the command against the problematic servers again. I didn’t end up using this as often as I expected, though.

I also wanted to be able to write very simple commands that used items from multiple stacks. In this example, I’m using rsync to back up three directories to two different remote servers:

Real world example: one of my backup scripts

I use Duplicity to make a backup of important parts of my home directory once a day. I have a script that runs right after this to copy those new backups out to a couple of servers out on the Internet. The script pulls those server names from a stack called rsyncDestinations:

My rsync backup script using shstack
#! /bin/bash

source ~/.keychain/zaphod-sh

s for "echo Syncing $SRC to %rsyncDestinations%;
       nice rsync -va --partial --delete --progress $SRC %rsyncDestinations%

The future of shstack

Shstack definitely has some bugs. Some of them are just cosmetic, while others are pretty serious. I was going to record a screen cast showing off how sfor works, but I immediately noticed that sfor doesn’t correctly handle file names with spaces. I was pretty surprised by this because I’m usually pretty good about making sure my code is space friendly.

Obviously, I’d like to fix some of these bugs. I’m sure I’ll find others along the way too. I’d also like to write some documentation. I don’t think the help screen is verbose enough to actually explain how to work it.