Shell script-produced Terminal session playback feature script sharing _linux shell

Source: Internet
Author: User

Not long ago I saw two interesting commands in the book--script and Srciptreplay, which can record a terminal session in a file, that is, we can create a command-line video tutorial through a terminal session, or we can share session files with others, And the generated file is just a general text file, the size of the file is very small, it is very interesting. Here are two shell programs I've written to facilitate this interesting and meaningful operation.

First, the implementation of the Code

File: record.sh

Copy Code code as follows:

#! /bin/bash
# Filename:Record.sh

Read-p "Please input the Sesson filename for you want to creat:" filename;

Sesfile= "$filename. Session"
Logfile= "$filename. Timing.log"

If [-e $sesfile];then
echo "$sesfile is exsit,creat session file fault!";
Read-p "If you want to reload the file?" [y/n]: "Flag;
If ["$flag" = "Y"];then
RM $sesfile $logfile;
Script-t 2> $logfile-a $sesfile;
Else
echo "Nothing to do!";
Fi

Else
Script-t 2> $logfile-a $sesfile;
Fi

File: replay.sh

Copy Code code as follows:

#! /bin/bash
# Filename:Replay.sh

Read-p "Please input the session filename:" filename
Logfile= "$filename. Timing.log"
Sesfile= "$filename. Session"
If [-e $sesfile]; Then
Scriptreplay $logfile $sesfile
Echo
Else
echo "$filename is not exsit!"
Fi

Second, the procedure analysis

First of all, the two files are used to do it, record.sh is used to record the commands you perform and the output of the screen, and replay.sh is used to playback record.sh recorded content.

In record.sh, you first need to enter a file name to hold the information, such as output, and then create two files in the program, file name for the file name you entered plus a different suffix form, such as Output.timing.log and output.session, and then check whether the file entered already exists, if it exists, ask if you want to overwrite the old file with the new file, delete the original file, create a new file and write the data, and if not, do nothing. Enter a filename in the replay.sh, first to determine if the file exists, and to play it.

In the record.sh, you can see that the script parameter is two files, in order to make it easier to identify, I added a specific suffix to the filename I entered, even on Linux, the file suffix is meaningless, one of the suffix's. Timing.log, used to store temporal information, describe when each command was run, and another file , the suffix is. session, which is used to store the command output. With these two files can realize playback function, this is a bit like everyone familiar with Windows LRC lyrics file playback function bar, personally feel.

And because there are two kinds of output, so it is necessary to use data stream redirection, different output streams output different files, in the record.sh we can also see that we use 2> to write time series information through stderr to file *.timing.log, through the > Writes the command execution information to the file *.session.

And replay.sh is relatively simple, it only needs to determine whether the input file exists, you can play. If you want to play the file made in the above record.sh, you just need to input output.

It's very interesting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.