#!/usr/bin/env bash # Variables #DRY=echo DRY= CLEAN=0 usage () { echo "Usage: $(basename $0) [-n] name" echo echo "-n - perform dry run, do nothing" } if [ $# -lt 1 ] ; then usage exit 1 fi while [ $# -gt 0 ] ; do # Store how many options we have now OPTCOUNT=$# OPT=$1 # Consume options if we recognise them if [ "$OPT" == "-n" ] ; then DRY=echo shift fi if [ "$OPT" == "-h" ] ; then usage exit 0 fi if [ "$OPT" == "-l" ] ; then tmux ls exit $? fi # Bit of a hack, but it works. if [ $# == $OPTCOUNT ] ; then break fi done if [ "$TMUX" != "" ] ; then echo "You're already in a tmux session; detach first." exit 1 fi TARGET=$1 shift if [ "$TARGET" == "" ] ; then echo "You didn't specify a target." usage exit 1 fi echo Looking for $TARGET MATCHCOUNT=$(tmux ls | grep "$TARGET" | wc -l) if [ $MATCHCOUNT -eq 0 ] ; then # Use the name as what they passed. SESSION_NAME="$TARGET" COMMAND="new -s $SESSION_NAME" elif [ $MATCHCOUNT -gt 1 ] ; then found_multiple=1 OLD_TARGET=$TARGET TARGET="^$OLD_TARGET:" MATCHCOUNT=$(tmux ls | grep "$TARGET" | wc -l) if [ $MATCHCOUNT -ne 1 ] ; then echo "Confused by session naming; $OLD_TARGET matches too many sessions." $DRY tmux ls | grep $OLD_TARGET exit 1 fi fi if [ $MATCHCOUNT -eq 1 ] ; then MATCH=$(tmux ls | grep "$TARGET") # Pull out the existing session name. SESSION_NAME=$(echo "$MATCH" | sed -e 's/:.*$//') COMMAND="at -t $SESSION_NAME" fi if [ "$COMMAND" != "" ] ; then $DRY tmux $COMMAND fi