Linux命令-scriptreplay(终端会话回放)
scriptreplay 是 script 命令的配套工具,用于按原始时间节奏回放由 script -t 录制的终端会话。它读取时序文件和输出文件,以与录制时相同的时间间隔重新呈现终端操作过程,是制作命令行教程和操作回溯的绝佳工具。
快速参考
bash
scriptreplay [选项] 时间文件 [输出文件] [倍率]
scriptreplay需要配合script -t使用。-t选项会生成一个时序文件,记录每次输出的时间间隔,scriptreplay据此还原播放节奏。
基本回放
bash
# 录制阶段(使用 script -t)
script -t 2> timing.log output.log
# ... 执行各种操作 ...
exit
# 回放阶段
scriptreplay timing.log output.log
# 等效写法(指定全路径)
scriptreplay timing.log output.log 1
控制回放速度
bash
# 2 倍速回放(加快播放)
scriptreplay timing.log output.log 2
# 0.5 倍速(慢放,适合学习)
scriptreplay timing.log output.log 0.5
# 4 倍速(快速浏览)
scriptreplay timing.log output.log 4
# 使用 -d 选项指定倍率(新版语法)
scriptreplay -d 2 timing.log output.log
# 使用 -m 选项限制最大暂停时间
# 如果录制中有长时间暂停,跳过超过3秒的等待
scriptreplay -m 3 timing.log output.log
回放指定终端类型
bash
# 指定终端类型
scriptreplay -t xterm timing.log output.log
scriptreplay -t screen timing.log output.log
scriptreplay -t linux timing.log output.log
# 在 tmux 窗格中回放
tmux new-window "scriptreplay timing.log output.log"
录制与回放完整流程
bash
#!/bin/bash
# 完整的录制-回放教程工具
# 用法: ./tutorial_tool.sh record|replay|list
BASE_DIR="./terminal_recordings"
case "${1:-help}" in
record)
NAME="${2:-session_$(date +%H%M%S)}"
SESSION_DIR="${BASE_DIR}/${NAME}"
mkdir -p "$SESSION_DIR"
OUTPUT="${SESSION_DIR}/output.log"
TIMING="${SESSION_DIR}/timing.log"
echo "========================================="
echo " 终端录制: $NAME"
echo " 时间: $(date)"
echo "========================================="
echo "输入 exit 或 Ctrl+D 结束录制"
echo ""
script -q -t 2>"$TIMING" "$OUTPUT"
echo ""
echo "录制已保存到: $SESSION_DIR"
echo "回放: $0 replay $NAME"
;;
replay)
NAME="$2"
if [ -z "$NAME" ]; then
echo "可用录制:"
ls "$BASE_DIR/" 2>/dev/null
exit 1
fi
SESSION_DIR="${BASE_DIR}/${NAME}"
SPEED="${3:-1}"
if [ ! -f "${SESSION_DIR}/timing.log" ]; then
echo "错误: 找不到录制 $NAME"
exit 1
fi
echo "回放: $NAME (${SPEED}x)"
scriptreplay "${SESSION_DIR}/timing.log" "${SESSION_DIR}/output.log" "$SPEED"
;;
list)
echo "可用录制:"
for dir in "$BASE_DIR"/*/; do
name=$(basename "$dir")
date=$(stat -c %y "$dir/output.log" 2>/dev/null | cut -d'.' -f1)
size=$(du -sh "$dir" 2>/dev/null | cut -f1)
echo " $name ($date, $size)"
done
;;
*)
echo "用法: $0 {record|replay|list}"
echo " record [名称] - 开始录制终端会话"
echo " replay <名称> [速度] - 回放录制"
echo " list - 列出所有录制"
;;
esac
制作教程录制最佳实践
bash
#!/bin/bash
# 教程录制最佳实践脚本
# 在录制前清屏、设定提示符
# 1. 设置简洁的提示符
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# 2. 清屏
clear
# 3. 开始录制
script -q -t 2> timing.log output.log
# 录制中:
# - 操作前先说明意图(用 echo 或注释)
# - 每步操作间隔适当停顿
# - 命令后查看结果
# - 结束前总结
# 4. 回放验证
# scriptreplay timing.log output.log 1.5 # 1.5倍速验证
与其他工具配合
bash
# 将回放内容输出到文件(去掉时间因素)
scriptreplay -d 1000 timing.log output.log > plain_output.txt
# 在录制中添加水印/标记
script -q -t 2> timing.log -c '
echo "===== 操作开始: $(date) ====="
echo "操作者: $(whoami)"
echo "主机: $(hostname)"
echo "============================"
# ... 实际操作 ...
echo "===== 操作结束 ====="
' output.log
# 批量回放多个录制
for dir in terminal_recordings/*/; do
name=$(basename "$dir")
echo "--- 回放: $name ---"
scriptreplay "${dir}/timing.log" "${dir}/output.log" 4 # 4倍速批量浏览
sleep 1
done
# 录制中模拟慢速操作
script -q -t 2> timing.log output.log
# 在命令之间加 sleep 来控制节奏
echo "第一步: 查看当前目录"
sleep 1
ls -la
sleep 1
echo "第二步: 创建文件"
sleep 1
touch demo.txt
sleep 1
echo "第三步: 查看文件"
sleep 1
cat demo.txt
exit
与 script 的时间文件格式
bash
# 时间文件格式说明(每行两个数字)
# 秒.微秒 字节数
# 例如:
# 0.123456 50 -> 等待 0.123 秒后输出 50 字节
# 0.001000 12 -> 等待 0.001 秒后输出 12 字节
# 查看时间文件内容
cat timing.log
# 0.000000 65
# 0.123456 23
# 2.500000 156
# ...
# 统计总录制时长
awk '{sum+=$1} END{printf "总时长: %.2f 秒\n", sum}' timing.log
# 统计总输出字节数
awk '{sum+=$2} END{printf "总输出: %d 字节\n", sum}' timing.log
# 分析操作密度(每秒输出字节数)
awk '{time+=$1; bytes+=$2} END{printf "平均速率: %.0f 字节/秒\n", bytes/time}' timing.log
选项速查
| 选项 | 说明 |
|---|---|
-d DIVISOR |
回放速度倍率(2=2倍速) |
-m MAX_WAIT |
最大等待时间(跳过长暂停) |
-s SPEED |
与 -d 功能相同 |
-t TERM |
指定终端类型 |
-V |
显示版本信息 |
scriptreplay将终端操作变成了可回放、可分发的"视频",在命令行教程制作中具有独特价值。结合-d加速和-m跳过长暂停,可以快速浏览长时间的录制内容。