Tuesday, January 11, 2011

how Dropbox saved my command line - extended

i came across a clever blog post on the 'tubes. it described how one could harness the file-syncing powers of Dropbox to keep one's bash shell config files all lined up across several machines. this was just the ticket to keep my mac laptop, my work imac, and my linux box command lines all in harmony with each other.


of course, i couldn't just leave well enough enough alone.


MacOSX hostname discrepancies

the first problem i ran into was reliably getting a machine's hostname on the MacOSX side. the network at the office insists on setting my imacs's hostname dynamically, and it's never the same between bootups. luckily i stumbled on a command that'll get the local hostname of a MacOSX computer:


Listing 1: getting the local hostname in MacOSX:

$ scutil --get LocalHostName


this will spit out whatever name you have set in System Preferences -> Sharing -> Computer Name, regardless of what the local networks says.


why not synced bin directories, too?

i have a handful of utility shell scripts that i had spread out across my different machines. similar to syncing up the config files, i came up with a way to consolidate all those scripts into a single shared ~/Dropbox/sh/bin directory. i also have a ~/Dropbox/sh/bin/Darwin directory for my MacOSX-specific scripts, and a ~/Dropbox/sh/bin/Linux for my Linux scripts, all of which my PATH environment variable appropriately points to. See Listing 2 to see it in action.


older Dropbox versions can't remember which scripts are executable

the official stable version of Dropbox on Linux maxes out at 0.7.x, which unfortunately doesn't sync the executable bit on files. that means all that effort getting my bin directories would be for naught. all is not lost! there exist Dropbox beta builds that go up to 1.0.10, which do support executable-bit syncing. the easiest way to get the latest dropbox beta builds for Linux is with a utility by the name of dbupdate. Note: if you have Dropbox installed for a single user (e.g. not system-wide, like how crunchbang linux does it by default) you'll have to run dbupdate manually from the command line. problem solved!


the goods

without further ado:


Listing 2: bashbootstrap:

# bashbootstrap
if [ -z "$PS1" ]; then
 return
fi

localhostname() {
 case $(uname) in
  "Darwin" )
  echo $(scutil --get LocalHostName)
  ;;
  "Linux" )
  echo $(hostname)
  ;;
  * )
  echo $HOSTNAME
  ;;
 esac
}

dropboxshelldir=~/Dropbox/sh
dropboxdir=$dropboxshelldir/etc

masterbashrc=$dropboxdir/bashrc
masterbashrcpost=$dropboxdir/bashrcpost
osbashrc=$masterbashrc-$(uname)
localbashrc=$osbashrc-$(localhostname)

masterbin=$dropboxshelldir/bin
osbin=$masterbin/$(uname)
localbin=$osbin/$(localhostname)

echo -n "Applicable shell configs: "
for bashfile in "$masterbashrc" "$osbashrc" "$localbashrc" "$masterbashrcpost"; do
 if [ -r $bashfile ]; then
  . $bashfile
  echo -n "$(basename $bashfile) "
 fi
done
echo

echo -n "Applicable bin directories: "
for bindir in "$masterbin" "$osbin" "$localbin"; do
 if [ -d $bindir ]; then
  PATH=$PATH:$bindir
  echo -n "$(basename $bindir) "
 fi
done
echo

# Set convenience aliases
myed=${VISUAL:-${EDITOR:-vim}}
alias editbashrc="$myed $masterbashrc"
alias editbashrcpost="$myed $masterbashrcpost"
alias editosbashrc="$myed $osbashrc"
alias editlocalbashrc="$myed $localbashrc"

a few odds-and-ends

  • MacOSX gets confused with .bash_profile and .bashrc, check bash_profile vs. bashrc for a solution
  • need some idea on how to spruce up you command line? lifehacker terminal tweaks is a good place to start
  • Don't have a Dropbox account yet? Why not use this dropbox referral link to get an extra 250MB of sync space gratis! (full disclosure: i'll get and extra 250MB of space, too =] )

No comments: