Not long ago, I saw two very interesting commands-script and srciptreplay, which can record terminal sessions to a file, that is, we can use terminal sessions to create command line skills video tutorials, you can also share session files with others, and the generated files are only common text files. The file size is very small and really interesting. The following are two Shell programs I have written to facilitate this interesting and meaningful operation. I. Implementation code file: record. Sh
#! /bin/bash# Filename:Record.shread -p "Please input the sesson filename you want to creat: " filename;sesfile="$filename.session"logfile="$filename.timing.log"if [ -e $sesfile ];thenecho "$sesfile is Exsit,Creat session file fault!";read -p "If you want to reload the file? [Y/N]: " flag;if [ "$flag" = "Y" ];thenrm $sesfile $logfile;script -t 2> $logfile -a $sesfile;elseecho "Nothing to do!";fielsescript -t 2> $logfile -a $sesfile;fi
File: replay. Sh
#! /bin/bash# Filename:Replay.shread -p "Please input the session filename: " filenamelogfile="$filename.timing.log"sesfile="$filename.session"if [ -e $sesfile ]; thenscriptreplay $logfile $sesfileechoelseecho "$filename is NOT Exsit!"fi
2. What are the two files used for program analysis? record. SH is used to record the commands you run and the output on the screen, while replay. SH is used to play back record. sh. In record. sh, you must first enter a file name for saving information, such as output, and then create two files in the program. The file name is formed by adding different suffixes to the file name you entered, such as output. timing. log and output. session, and then check whether the input file already exists. If yes, check whether the new file overwrites the old one. If yes, delete the original file, create a new file and write data. If no, no operation is performed. Input a file name in replay. Sh. First, judge whether the file exists and play the video if it exists. In record. sh, we can see that the script parameter is two files. To facilitate identification, I added a specific suffix to the input file name. Even on Linux, the file suffix is meaningless, one of the suffixes. timing. log is used to store time sequence information, which describes when each command is run, and the other file is suffixed. session, used to store command output. With these two files, you can implement the playing function, which is a bit like the playing function of LRC lyrics files in windows. I personally think. Because there are two types of output, we need to use data stream redirection to output different output streams to different files in record. sh, we can also see that we use 2> write time sequence information to a file through stderr *. timing. log, write the command execution information to the file through> *. session. Replay. Sh is relatively simple. It only needs to determine whether the input file exists and can be played. If you want to play the file created in record. Sh above, you only need to input output. Very interesting!