A fascinating thing about humans is that we each have a particular learning style. Some people prefer to read about how something's done. Some people prefer to follow instructions from the start, while others prefer to get an overview before engaging. Other people like to listen to instructions from an instructor or a podcast. And yet another group wants instructions in the form of a video.

The trouble with video is that it can be difficult to produce and compress, and it is a lot of work to change once it's done. And besides that, viewers can't copy and paste Linux commands out of a video into their terminal.

Or can they?

The script and scriptreplay commands allow you to record a terminal session and then play it back later. There's no video involved. Instead, plain text files called typescripts are used, so the files are small and users can copy commands during playback.

Both script and scriptreplay commands are valuable documentation tools, and I find them useful in two scenarios:

  1. No matter how sincerely I promise myself that I'll go through my shell history and gather the proper commands once I figure out the right way to accomplish a task, I rarely do.
  2. Sometimes, my users don't want to see the input required of them; they want to see the interaction between them and the computer. You can tell a user to type ls and describe what they can expect to see in response, but it makes a greater impact when they see it happening themselves. I find users a lot braver in the terminal after playing a script file to see what it's like first.

[ Register for Ansible Basics: Automation Technical Overview and learn configuration management, provisioning, deploying, and managing compute infrastructure across environments with Ansible. ]

Record your terminal

To start recording with script, provide two destination files: a timing file and a typescript file:

$ script --timing=time.tm myscript

Output alerts you that script has started recording. Everything you do in your terminal while script is active is recorded. This includes everything going to stdin, stdout, and stderr, including your pauses, backspaces, auto-completions, and even your mistakes.

Press Ctrl+D or type exit to stop the recording when you've finished your demonstration.

The resulting typescript file, myscript in this example, is straightforward and relatively self-documenting:

Script started on 2022-01-06 22:27:01+13:00
0;seth:~/script^G$ echo "hello world"
hello world
0;seth:~/script^G$ exit
exit

Script done on 2022-01-06 22:27:10+13:00

You can edit this file. but beware of making too many changes because the output's timing has also been recorded.

Still, to correct minor errors, a quick edit with a text editor is an easy fix.

Playing a script

You can play a recorded script using the scriptreplay command:

$ scriptreplay --timing=time.tm myscript

This takes over your terminal session and uses it as a canvas to replay the input and output of what you recorded. The timing is replicated, too, so meaningful pauses for clarity are retained, which can be helpful to a user following along with the script playback.

Of course, the commands playing back in the terminal aren't being executed. It's just an instant replay, so it's safe to play scripts that delete files, make Git commits, or launch Ansible plays. Nothing in the playback of a script actually happens. It's just a movie.

Documentation in action

The script and scriptreplay commands are valuable tools for the "show don't tell" philosophy. Whether your target audience is you at some point in the future, long after you've forgotten what you were sure you'd never forget, or a timid new terminal user, seeing human-computer interaction can be illuminating and empowering.

Unlike screen captures, typescripts are small in file size and easy to update and edit. Unlike your shell history, typescripts aren't rotating. They don't go away unless you remove them yourself. And unlike shell scripts, a typescript demonstrates the steps without performing the process, encouraging the viewer to try each step, internalize it and commit it to memory, and gain a better understanding of what the terminal is truly capable of doing.


关于作者

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来