Using GNU Screen to perform a command line demo
A few weeks ago, I saw videos of developers at the Apple WWDC conference doing their presentations with a clipboard tool within OSX, which pastes snippets of code from a queue. Developers at the conference do not need to remember or refer to a document to provide an example. Their focus is only on the presentation.
Today, I gave a presentation to a group of developers showing off some capabilities of different tools from the command-line. I find it hard thinking about what I’m going to say in a public forum and thinking of command line arguments at the same time. My presenter hat comes off and I put on my engineering hat. One solution I came up with in Linux is to use the Screen command line tool to pass commands to a running Screen session (created with `screen -S demo`). This script runs in a different terminal on my laptop display, and all I have to do is press enter throughout the presentation.
The presentation today was a royal success. Partly due to this script.
Update: I was asked to explain the following code snippet. The code sends commands to a screen session called ‘demo’. Created with the command above. It then loops through the command list and if the window is 0 pauses after each command to allow for explanation. If the window number is not zero it will execute the command without interaction in the screen window specified.
#!/usr/bin/env python
from subprocess import Popen
# set with -S on the screen commandline
screen_name = 'demo'
# window specifies the screen window to direct the command
commands = [
{
'window':0,
'command':'ls -l',
'post-command':'\n',
},
{
'window':0,
'command':'echo Hello World',
'post-command':'\n',
},
# direct this command to window 1
{
'window':1,
'command':'echo Hello from window 1',
'post-command':'\n',
},
]
def run_screen(screen_name, window, cmd):
Popen(['screen', '-S', screen_name, '-p', str(window), '-X', 'stuff',
cmd]).communicate()
for cmd in commands:
if cmd['window'] == 0:
print("(%(window)i) %(command)s" % cmd)
run_screen(screen_name, cmd['window'], cmd['command'])
if cmd['post-command']:
print("+ %s" % cmd['post-command'].replace('\n', '<cr>'))
raw_input('...waiting for keypress')
run_screen(screen_name, cmd['window'], cmd['post-command'])
print(".... talk about whats happening ....")
raw_input()
else:
run_screen(screen_name, cmd['window'], cmd['command'])
run_screen(screen_name, cmd['window'], cmd['post-command'])








