Skip to content

Commit

Permalink
Add option to initialize a fresh screen session
Browse files Browse the repository at this point in the history
to do_on_screen.sh
-C number-of-windows-to-create

Removes the requirement to create a screen session
before using do_on_screen.sh
  • Loading branch information
samn committed May 7, 2012
1 parent 223e0fd commit 7e06424
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions scriptorium/do_on_screen.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
#! /bin/bash
# TODO:
# - option to create missing windows
# actually, this is a bug
# it should ensure that all windows requested
# are available.
#
# - Should clear the current line before sending a command
# since there could be junk on the command line.

function print_usage() {
echo "$0 -S <screen_name> -w <windows,> <-c <command> || -r>"
echo "$0 -S <screen_name> [-C number of windows] -w <windows,> <-c <command> || -r>"
echo "Send a command to multiple windows of a Screen session"
echo "Required Params:"
echo "-S the name of the screen to affect"
echo "-w a comma separated list of windows to send input to"
echo "One of the following"
echo "-c the command to send to each window"
echo "-r read stdin and send to each window"
echo "Optional Params:"
echo "-C how many windows to initialize. (initializes a new session)"
}

NAME=
WINDOWS=
COMMAND=
READ=
SHOULD_INITIALIZE=
INITIALIZE=

while getopts ":S:w:c:r" OPTION; do
while getopts ":S:w:c:rC:" OPTION; do
case $OPTION in
S)
NAME=$OPTARG
Expand All @@ -38,24 +37,37 @@ while getopts ":S:w:c:r" OPTION; do
r)
READ=true
;;
C)
SHOULD_INITIALIZE=true
INITIALIZE=$OPTARG
;;
?)
print_usage
exit
;;
esac
done

if [[ -z "$NAME" ]] || [[ -z "$WINDOWS" ]] || [ -z "$COMMAND" -a -z "$READ" ] || [ -n "$COMMAND" -a -n "$READ" ]
then
if [[ -z "$NAME" ]] || [[ -z "$WINDOWS" ]] || [ -z "$COMMAND" -a -z "$READ" ] ||
[ -n "$COMMAND" -a -n "$READ" ] || [ -n "$SHOULD_INITIALIZE" -a -z "$INITIALIZE" ] ; then
print_usage
exit
fi

# check that a screen session with name $NAME exists
screen -S $NAME -X -p 0 wall "$0 is running on windows $WINDOWS"
if [[ $? -ne 0 ]] ; then
echo "Error: Screen with name $NAME not found"
exit
if [[ -n "$SHOULD_INITIALIZE" ]] ; then
# create a new, detatched, session
screen -S $NAME -dm
# and open up some windows
for a in `seq 1 $INITIALIZE`; do
screen -S $NAME -X screen $a
done
else
# check that a screen session with name $NAME exists
screen -S $NAME -X -p 0 wall "$0 is running on windows $WINDOWS"
if [[ $? -ne 0 ]] ; then
echo "Error: Screen with name $NAME not found"
exit
fi
fi

function run_on_screen() {
Expand Down

0 comments on commit 7e06424

Please sign in to comment.