On Tue-2011/01/25-14:23 hubert depesz lubaczewski wrote in
<20110125132342.ga18...@depesz.com>:

> To help me with it, I wrote simple script (attached to this mail), which
> perhaps you will find useful.

Minor nits about quoting:  both $((...)) and subscript [...] in bash
treat their arguments as numbers, $(...) (command substitution) is
implicitly quoted on the outside, which eases quotes on the inside.
Also, lines can be separated at pipes.

#+v
--- tmux-auto-reattach.0        2011-01-28 21:17:50.617641902 +0100
+++ tmux-auto-reattach  2011-01-28 21:32:50.192984908 +0100
@@ -82,11 +82,11 @@
             show_help_and_exit "Unknown option -%s" "$OPTARG"
             ;;
     esac
 done

-shift $(( $OPTIND - 1 ))
+shift $(( OPTIND - 1 ))
 if [[ "$1" = -- ]]
 then
     shift
 fi

@@ -98,11 +98,11 @@
 if [[ -z "$session_name" ]]
 then
     session_name="${1##*/}"
 fi

-stringified_command_for_tmux="$( printf '%q ' "$@" )"
+stringified_command_for_tmux=$( printf '%q ' "$@" )

 if (( "$singleton" == 1 ))
 then
     if tmux has -t "$session_name" > /dev/null 2>&1
     then
@@ -114,11 +114,14 @@
     # but since I don't care about such characters in arguments to normal
     # tmuxable programs:
     exec tmux new-session -s "$session_name" "exec 
$stringified_command_for_tmux"
 fi

-interesting_sessions_list="$( LC_ALL=C tmux list-sessions 2> /dev/null | grep 
-E "^${session_name}(-[0-9]+)?:" )"
+interesting_sessions_list=$(
+    LC_ALL=C tmux list-sessions 2> /dev/null |
+    grep -E "^${session_name}(-[0-9]+)?:"
+    )

 if [[ -z "$interesting_sessions_list" ]]
 then
     exec tmux new-session -s "$session_name" "exec 
$stringified_command_for_tmux"
 fi
@@ -130,22 +133,23 @@
     just_name="${session_line%%:*}"
     [[ "$just_name" =~ $matching_regex ]] || continue
     number_in_name="${just_name##*-}"
     if (( $number_in_name >= $potential_new_session_number ))
     then
-        potential_new_session_number=$(( $number_in_name + 1 ))
+        potential_new_session_number=$(( number_in_name + 1 ))
     fi
 done <<< "$interesting_sessions_list"

-detached_sessions="$( echo "$interesting_sessions_list" | grep -vE ' 
\(attached\)$' )"
+detached_sessions=$( echo "$interesting_sessions_list" | grep -vE ' 
\(attached\)$' )

 if [[ -z "$detached_sessions" ]]
 then
-    exec tmux new-session -s "${session_name}-${potential_new_session_number}" 
"exec $stringified_command_for_tmux"
+    exec tmux new-session -s "${session_name}-${potential_new_session_number}" 
\
+        "exec $stringified_command_for_tmux"
 fi

-detached_sessions_count="$( echo "$detached_sessions" | wc -l )"
+detached_sessions_count=$( echo "$detached_sessions" | wc -l )

 if (( "$detached_sessions_count" == 1 ))
 then
     exec tmux attach-session -t "${detached_sessions%%:*}"
 fi
@@ -160,28 +164,30 @@
     this_session_name="${line%%:*}"
     session_names[$i]="$this_session_name"

     suffix_removed="${line%)*}"
     session_text="${suffix_removed##*(}"
-    whiptail_options[$(( $i * 2 ))]="$i."
-    whiptail_options[$(( $i * 2 + 1))]="$session_text"
+    whiptail_options[i * 2]="$i."
+    whiptail_options[i * 2 + 1]="$session_text"
 done <<< "$detached_sessions"

-whiptail_output="$( mktemp )"
+whiptail_output=$( mktemp )
 trap "rm -f $whiptail_output" EXIT
-whiptail --title "Choose session to connect to:" --menu "" 17 41 10 
"${whiptail_options[@]}" 2> "$whiptail_output"
+whiptail --title "Choose session to connect to:" --menu "" 17 41 10 \
+    "${whiptail_options[@]}" 2> "$whiptail_output"
 user_choice=$( < "$whiptail_output" )
 rm -f "$whiptail_output"

 if [[ -z $user_choice ]]
 then
     exit
 fi

 if [[ $user_choice == "0." ]]
 then
-    exec tmux new-session -s "${session_name}-${potential_new_session_number}" 
"exec $stringified_command_for_tmux"
+    exec tmux new-session -s "${session_name}-${potential_new_session_number}" 
\
+        "exec $stringified_command_for_tmux"
 fi

 session_index="${user_choice%.}"

 exec tmux attach-session -t "${session_names[$session_index]}"
#-v


clemens


------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to