diff --git a/hostmux b/hostmux index 3e3b44f..486c3a7 100755 --- a/hostmux +++ b/hostmux @@ -1,6 +1,8 @@ #!/bin/sh # Author: hukl # Repo: https://github.com/hukl/hostmux +# shellcheck disable=SC2162 +# we can disable SC2162 here, we want to allow backslashes in the host file USAGE="hostmux -h | [-xpPa] [-c ] [-o ] [-l ] [-s ] [-f ] host1 [host2 ...]" @@ -12,13 +14,14 @@ SET_PROMPT=false SYNC_PANES=false SSH_CMD=ssh SSH_OPTS= +TMUX_CMD=tmux # Get optional user settings while getopts s:l:f:c:o:hxpPa opt; do case $opt in - s) SESSION_NAME=$OPTARG + s) SESSION_NAME="$OPTARG" ;; - l) LAYOUT=$OPTARG + l) LAYOUT="$OPTARG" ;; x) CLOSE=" && exit" ;; @@ -28,17 +31,17 @@ while getopts s:l:f:c:o:hxpPa opt; do ;; a) SYNC_PANES=true ;; - c) SSH_CMD=$OPTARG + c) SSH_CMD="$OPTARG" ;; - o) SSH_OPTS=$OPTARG + o) SSH_OPTS="$OPTARG" ;; - f) HOSTS_FILE=$OPTARG + f) HOSTS_FILE="$OPTARG" ;; - h) echo $USAGE + h) echo "$USAGE" exit 0 ;; '?') echo "$0: invalid option -$OPTARG" >&2 - echo $USAGE >&2 + echo "$USAGE" >&2 exit 1 ;; esac done @@ -48,10 +51,10 @@ NUMBER_OF_HOSTS=0 NUMBER_OF_HOST_ARGS=$# HOSTS="" -while [ $NUMBER_OF_HOSTS -lt $NUMBER_OF_HOST_ARGS ] +while [ "$NUMBER_OF_HOSTS" -lt "$NUMBER_OF_HOST_ARGS" ] do HOSTS="$HOSTS$1 " - NUMBER_OF_HOSTS=$(($NUMBER_OF_HOSTS+1)) + NUMBER_OF_HOSTS=$((NUMBER_OF_HOSTS+1)) shift done @@ -61,34 +64,34 @@ then while read line do HOSTS="$HOSTS$line " - NUMBER_OF_HOSTS=$(($NUMBER_OF_HOSTS+1)) - done < $HOSTS_FILE + NUMBER_OF_HOSTS=$((NUMBER_OF_HOSTS+1)) + done < "$HOSTS_FILE" fi -echo $HOSTS -echo $NUMBER_OF_HOSTS +echo "$HOSTS" +echo "$NUMBER_OF_HOSTS" # Initialize Session -tmux -2 new-session -d -s $SESSION_NAME +"$TMUX_CMD" -2 new-session -d -s "$SESSION_NAME" # Split as many times as we have hosts and reset # the layout each time for even distribution LOOP_INDEX=1 while [ $LOOP_INDEX -lt $NUMBER_OF_HOSTS ]; do - tmux split-window -h -d - tmux select-layout $LAYOUT - LOOP_INDEX=$(($LOOP_INDEX+1)) + "$TMUX_CMD" split-window -h -d + "$TMUX_CMD" select-layout "$LAYOUT" + LOOP_INDEX=$((LOOP_INDEX+1)) done # Check what pane-base-index is configured and use it for addressing # the panes -PANE_INDEX=$(tmux show-window-options -g -v pane-base-index) +PANE_INDEX=$("$TMUX_CMD" show-window-options -g -v pane-base-index) # http://stackoverflow.com/questions/9747952/pane-title-in-tmux if $SET_PANE_TITLE; then - tmux set-window-option -g pane-border-status top - tmux set-window-option -g pane-border-format " #{pane_index} [#{pane_title}] " + "$TMUX_CMD" set-window-option -g pane-border-status top + "$TMUX_CMD" set-window-option -g pane-border-format " #{pane_index} [#{pane_title}] " fi # Loop through the panes and take the first argument as a ssh host @@ -97,19 +100,19 @@ fi # to the script for host in $HOSTS do - tmux send-keys -t $PANE_INDEX "$SSH_CMD $SSH_OPTS $host $CLOSE" C-m + "$TMUX_CMD" send-keys -t "$PANE_INDEX" "$SSH_CMD $SSH_OPTS $host $CLOSE" C-m if $SET_PROMPT; then - tmux send-keys -t $PANE_INDEX " export PS1=\"[$host]$ \"" C-m + "$TMUX_CMD" send-keys -t "$PANE_INDEX" " export PS1=\"[$host]$ \"" C-m fi if $SET_PANE_TITLE; then - tmux send-keys -t $PANE_INDEX " unset PROMPT_COMMAND; printf '\033]2;$host\007'" C-m + "$TMUX_CMD" send-keys -t "$PANE_INDEX" " unset PROMPT_COMMAND; printf '\033]2;$host\007'" C-m fi - PANE_INDEX=$(($PANE_INDEX+1)) + PANE_INDEX=$((PANE_INDEX+1)) done if $SYNC_PANES; then - tmux set-window-option synchronize-panes on + "$TMUX_CMD" set-window-option synchronize-panes on fi # Attach to the session -tmux -2 attach-session -t $SESSION_NAME +"$TMUX_CMD" -2 attach-session -t "$SESSION_NAME"