CS 3358 (Data Structures and Algorithms) by Lee S. Koh

Using script to Capture Linux Screen Displays as Text

(With many screenfuls of text, such as test results for a set of test cases, it'd be tedious and time consuming to screen capture.)
(To get test results for a set of test cases, you'd usually use input/output redirection, i.e., do screen capture only as last resort.)

The Linux's script facility can be used to capture (record) interactive screen displays as text
It is a very basic tool for capturing the screen displays of (a portion of) a user's interactive session (such as testing a program) under the Linux command-line environment. 
Being a very basic tool, script copies everything (including end-of-line markers, separators, etc. that often show up as funky characters in text) that is typed in by the user (and echoed on the screen) or written to the screen by the system or a program.










Features/usage summary: 
Level 1 Bullet The user activates the facility at the point where (s)he wants capturing to start.

Level 2 Bullet Typically, the user would do so by entering


script filename


where filename is the name of the text file that the user wants the captured output stored.

Level 2 Bullet If the user does not provide a file name (i.e., enters only script), the default file name typescript will be used.

Level 2 Bullet The facility will signal that it is activated by displaying


Script started, output file is filename


where filename is the file name that the user provides or typescript if the user chooses to use the default file name.
Level 1 Bullet The user deactivates the facility at the point where (s)he wants capturing to end.

Level 2 Bullet The user can do so either by entering


exit


or typing


Ctrl-D


(pressing the D key while holding down the Ctrl key).

Level 2 Bullet The facility will signal that it is deactivated by displaying


Script done, output file is filename


where filename is the file name that the user provides or typescript if the user chooses to use the default file name.
Level 1 Bullet Everything that has been displayed to screen between the activation point and the deactivation point will be captured to a text file named filename (if the user provides a custom file name) or typescript (if the user chooses to use the default file name).










Example:
Level 1 Bullet Capturing (to a file named capture1.txt) a session that lists the files in the current directory, compiles the hello.cpp program, lists the files again, and runs the compiled program:

[lk04@nyx scratch]$ script capture1.txt
Script started, file is capture1.txt
[lk04@nyx scratch]$ ls
capture1.txt  hello.cpp
[lk04@nyx scratch]$ g++ -Wall -ansi -pedantic hello.cpp
[lk04@nyx scratch]$ ls
a.out  capture1.txt  hello.cpp
[lk04@nyx scratch]$ ./a.out
Enter your first name: John
Enter your last name: Wayne
Hello John Wayne!
[lk04@nyx scratch]$ exit
exit
Script done, file is capture1.txt
[lk04@nyx scratch]$ 
Level 1 Bullet Contents of capture1.txt (as seen in the vim editor):

Script started on Thu 05 Jan 2012 06:24:56 PM CST
[lk04@nyx scratch]$ ls^M
^[[00m^[[00mcapture1.txt^[[00m  ^[[00mhello.cpp^[[00m^M
^[[m[lk04@nyx scratch]$ g++ -Wall -ansi -pedantic hello.cpp^M
[lk04@nyx scratch]$ ls^M
^[[00m^[[01;32ma.out^[[00m  ^[[00mcapture1.txt^[[00m  ^[[00mhello.cpp^[[00m^M
^[[m[lk04@nyx scratch]$ ./a.out^M
Enter your first name: John^M
Enter your last name: Wayne^M
Hello John Wayne!^M
[lk04@nyx scratch]$ exit^M
exit^M

Script done on Thu 05 Jan 2012 06:25:51 PM CST










Shortcomings and how to make the best of it:
Level 1 Bullet As can be seen from the example above, script captures everything that is displayed on the screen, including separators and end-of-line markers that translate into funky characters (highlighted in red in the example) in text.
Level 1 Bullet Most (if not all) of the funky characters can be filtered out using the command

sed -r 's/\^(\[){0,2}[0-9]{0,2}(;[0-9]{0,2})?[m|M]//g' rawFilename | col -b > filteredFilename

where rawFilename is the name of the file with unfiltered contents and filteredFilename is the name of the file to store the filtered version.

For instance, to filter the raw contents of capture1.txt above and store the filtered version in another file named cap1.txt, the command to use is as follows:

sed -r 's/\^(\[){0,2}[0-9]{0,2}(;[0-9]{0,2})?[m|M]//g' capture1.txt | col -b > cap1.txt

The following shows the contents of cap1.txt (as seen in the vim editor):

Script started on Thu 05 Jan 2012 06:24:56 PM CST
[lk04@nyx scratch]$ ls
capture1.txt  hello.cpp
[lk04@nyx scratch]$ g++ -Wall -ansi -pedantic hello.cpp
[lk04@nyx scratch]$ ls
a.out  capture1.txt  hello.cpp
[lk04@nyx scratch]$ ./a.out
Enter your first name: John
Enter your last name: Wayne
Hello John Wayne!
[lk04@nyx scratch]$ exit
exit
Script done on Thu 05 Jan 2012 06:25:51 PM CST


Use your browser's Back button to go back to previous page. (click here to return to my homepage)