| /tools/screen-find.sh r1910 | /tools/screen-find.sh r1911 | ||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
|---|---|---|---|
| 2 | # screen finder... thing. | 2 | # screen finder... thing. |
| 3 | # | 3 | # |
| 4 | # Greps the current screen of each current screen session for text. | 4 | # Greps the current screen of each current screen session for text. |
| 5 | # If only one match is found, then it will do one of two things: | 5 | # If only one match is found, then it will do one of two things: |
| 6 | # 1) If the screen is already attached somewhere, then we will make that screen | 6 | # 1) If the screen is already attached somewhere, then we will make that screen |
| 7 | # blink so you see it. | 7 | # blink so you see it. |
| 8 | # 2) If the screen is detached, then we will attach now. | 8 | # 2) If the screen is detached, then we will attach now. |
| 9 | # | 9 | # |
| 10 | # If multiple are found, each screen session name is output. | 10 | # If multiple are found, each screen session name is output. |
| 11 | # | 11 | # |
| 12 | # Usage: | 12 | # Usage: |
| 13 | # ./sfind.sh <pattern> | 13 | # ./sfind.sh <pattern> |
| 14 | 14 | ||
| 15 | windowcat() { | 15 | windowcat() { |
| 16 | tmp=`mktemp` | 16 | tmp=`mktemp` |
| 17 | STY=$1 screen -X hardcopy $tmp | 17 | STY=$1 screen -X hardcopy $tmp |
| 18 | cat $tmp | 18 | cat $tmp |
| 19 | rm $tmp | 19 | rm $tmp |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | screenlist() { | 22 | screenlist() { |
| 23 | screen -ls | grep '^ ' | awk '{print $1}' | 23 | screen -ls | grep '^ ' | awk '{print $1}' |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | findscreen() { | 26 | findscreen() { |
| 27 | for other in `screenlist`; do | 27 | for other in `screenlist`; do |
| 28 | # Only check screens that are not the current screen | 28 | # Only check screens that are not the current screen |
| 29 | if [ "$STY" != "$other" ] ; then | 29 | if [ "$STY" != "$other" ] ; then |
| 30 | if windowcat $other | egrep -q "$1" ; then | 30 | if windowcat $other | egrep -q "$1" ; then |
| 31 | echo $other | 31 | echo $other |
| 32 | fi | 32 | fi |
| 33 | fi | 33 | fi |
| 34 | done | 34 | done |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | activate() { | 37 | activate() { |
| 38 | s=$1 | 38 | s=$1 |
| 39 | status=`screen -ls $s | grep $s | tr -d '()' | awk '{print $NF}'` | 39 | status=`screen -ls $s | grep $s | tr -d '()' | awk '{print $NF}'` |
| 40 | 40 | ||
| 41 | case $status in | 41 | case $status in |
| 42 | Attached) | 42 | Attached) |
| 43 | echo "$s" | ||
| 43 | for blink in 1 2 3 4 5; do | 44 | for blink in 1 2 3 4 5; do |
| 44 | STY="$s" screen -X select - | 45 | STY="$s" screen -X select - |
| 45 | sleep .05 | 46 | sleep .05 |
| 46 | STY=$s screen -X select 0 | 47 | STY=$s screen -X select 0 |
| 47 | sleep .05 | 48 | sleep .05 |
| 48 | done | 49 | done |
| 49 | ;; | 50 | ;; |
| 50 | Detached) | 51 | Detached) |
| 51 | screen -x $s | 52 | screen -x $s |
| 52 | ;; | 53 | ;; |
| 53 | *) | 54 | *) |
| 54 | echo "Unknown status $status on screen $s" | 55 | echo "Unknown status $status on screen $s" |
| 55 | ;; | 56 | ;; |
| 56 | esac | 57 | esac |
| 57 | } | 58 | } |
| 58 | 59 | ||
| 59 | screens=`findscreen "$@"` | 60 | screens=`findscreen "$@"` |
| 60 | 61 | ||
| 61 | found=`echo -n "$screens" | wc -w` | 62 | found=`echo -n "$screens" | wc -w` |
| 62 | 63 | ||
| 63 | case $found in | 64 | case $found in |
| 64 | 0) echo "None found." ;; | 65 | 0) echo "None found." ;; |
| 65 | 1) | 66 | 1) |
| 66 | activate $screens | 67 | activate $screens |
| 67 | ;; | 68 | ;; |
| 68 | *) | 69 | *) |
| 69 | echo "Multiple found:" | 70 | echo "Multiple found:" |
| 70 | echo "$screens" | 71 | echo "$screens" |
| 71 | esac | 72 | esac |