What I've Been Programming Lately: Bash Scripts

November 15, 2009

As if I wasn't already in love with POSIX shells, the more Bash scripts I write, the more I love it. Thank you thank you thank you, Apple, for including a (mostly) standard Bash shell in OS X!

One of the latest scripts that I wrote is simple but extremely useful to me. It moves the most recent file in my ~/Downloads directory to the current directory. Simple, and it sure started that way, but I've tweaked it to work quite nicely. The default behavior is to tell me what the most recent file is and ask me if I want to move it ('y' moves it and any other key cancels). This helps me if I forget that I downloaded some other things since the one I wanted to move. It also keeps track of a history, and allows for undoing the last move (if I do mess up). Also, it has decent error handling to ensure that I don't accidentally overwrite an existing file or such. This script is called mvdl, and I may post it on Google Code or Github sometime soon.

The other script I've been having fun perfecting is called backupwithgit. After my recent infatuation with Git, I've been versioning like crazy. backupwithgit helps me version not-too-critical directories, like my ~/random-textfiles directory. I can set a few options in the script, then make a symlink called backuptxt (or whatever I want), and then anytime I type 'backuptxt' in the terminal from any directory, it'll add and commit everything in my ~/random-textfiles directory. To deal with another not-too-critical directory, I set a couple more options in backupwithgit and then make a new symlink with a different name, and I'm good to go again. Like mvdl, this script will also be showing up on Google Code or Github sometime soon-ish.

I honestly don't know if these scripts would be useful to anyone other than myself, but I'm perfectly willing to share them once I clean them up a little bit. (Honest... just a little bit. They're quite nice as is.) If you're actually interested and can't wait for me to clean them up and post them, let me know and I'll hook you up, although they're pretty trivial scripts.

(If you've read this far through this post, then you might be interested to know that I'm quite disappointed with Stargate Universe. It seems as though they're explicitly trying to Battlestar Galactica-ize the Stargate franchise.)

9 comments: to “ What I've Been Programming Lately: Bash Scripts

  • joem
    November 19, 2009 at 12:53 PM  

    In case anyone cares, they're both now up at github: mvdl and backupwithgit

  • Dan Gr
    November 19, 2009 at 4:52 PM  

    I prefer zsh to bash. don't know why, but I'm sure there are many good reasons. The best script I've written (and the only one I use on a regular basis; and actually, now that I think about it, isn't even a script, it's just a looong alias) is "purr" which clears all backup files (*.~ and #*#) from the current directory and all subdirectories.

    What exactly is Git? I've seen lots of mention of it 'round the internet. Is it better than SVN (which is what I currently use)?

  • joem
    November 19, 2009 at 11:47 PM  

    I've heard good things about zsh from a lot of people. One day I might check it out, but I'm fine with bash for now.

    On git vs svn, git is a distributed system, in that each user has a complete copy of the repository data stored locally. This helps with speed, and it means that if there are multiple users who check something out, you have multiple back ups of your repository. Also, in git, branches are very easy and useful. It's trivial to make a new branch to test something out and then either delete it or merge it (or parts of it) back into your master branch. Also, apparently git takes up less room than subversion. Also (and this is less a feature of git itself, but still involves git), github is a pretty cool host for git repositories. Public repos are free, and private ones cost a little. But I only need public ones right now, so it's cool. It's super easy to push your commits to github once you've got your ssh key set up.

    For more details about git vs svn, check out this article.

    If you're happy with svn, there's no shame in sticking with it. Svn definitely has more GUIs and program support (I'm a little bummed that Xcode supports svn out of the box and not git), since svn's older and more established, but command line git is solid. I think you're a command line kind of guy anyway, right?

  • Dan Gr
    November 20, 2009 at 6:31 AM  

    Someone set me up with zsh when I first started using unix regularly, and I've always stuck with it.

    I'll have to check out git. I see that it's installed on my work system. I actually end up using rapidsvn often, because it's easier to see if I've forgotten to version some files. But I always checkin or update from the command line.

  • Anonymous
    December 14, 2009 at 6:30 AM  

    I was going to suggest that you use zsh instead of bash, but someone already beat me to the punch. Zsh is to bash as OS X is to Windows.

    Why don’t you use Time Machine to backup? You should also definitely backup to somewhere other than your computer. If the drive crashes, or the OS writes zeroes over the whole disk because it screws up, or your house burns down in a fire, you’ll still lose everything. You can open an Amazon S3 account and write a script to automatically push your backups there so they’re off-site.

  • Anonymous
    December 14, 2009 at 6:32 AM  

    I will definitely be commenting on your blog more often now that it lets me use my OpenID.

  • Anonymous
    December 14, 2009 at 6:33 AM  

    Except I’m slightly perturbed that it can’t seem to use my real name as my handle.

  • joem
    December 15, 2009 at 11:33 AM  

    re:Time Machine - I've never had an external drive with any free space.

    re:offsite backups - The backupwithgit script I wrote actually easily lets me push commits to a remote repo, using a -p argument. So I've been using Dropbox to that end. But it's true: I should have some sort of whole-system backup in place. I'm slowly going from "if i lose anything, it's a chance to start fresh" to "I don't want to lose anything ever again."

    re:zsh - Could I get the Cody-Cliff-Notes to why zsh is good?

  • Dan Gr
    December 15, 2009 at 12:53 PM  

    Joe, you need to investigate using Time Machine with a remote harddrive. And when you figure out the answer, let me know. I have an external hd attached to a linux box that I keep next to my router, and it serves as a network storage unit --- I really want to use time machine to back up to that networked ext hd.

    one thing that zsh has, which bash doesn't, is extensive history of directories you were in, so you can do more than just 'cd -' but also 'cd -2' etc. I never use it though.

/* Google Analytics ----------------------------------------------- */