Have you ever wanted to save a terminal session? If you learn how to do an arcane procedure you do not repeat often, you might want to save a sample terminal session in a file and then later you can refer to it when you want to repeat the procedure.
If you are teaching and use UNIX, you might want your students to have access to the terminal session to look at later.
Using script The command script will start a new shell and place the terminal session that shell in a written record. By default, this is called typescript. You can optionally specify a file name, and the typescript will go into that file. The -a option open the file in append mode, and places your terminal session at the end of the file.
Ending To end recording the terminal session, type exit at the UNIX prompt. This will kill the shell spawned by script and save it to the file you specified.
Uh Oh, What's all this garbage? The script command is like a barnyard pig. It "eats" everything! Your file will be festooned with backspace, linefeed and other ugly control characters.
Make it go away! If you do not already have one, make a bin directory in your home directory. This is the best place to store shell scripts you use to save work. Now create this shell script in that directory. Place it in a file called cleanScript.sh.
#!/bin/bash cat $1 | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $1.out
Now give the script execute permissions as follows.
$ chmod +x cleanScript.sh
To use it on the file foo proceed as follows.
$ cleanScript.sh foo $ ls (files) foo.out (more files)
The file foo.out will be your cleaned-up terminal session.
What if I want to display my terminal session on an HTML page? You will want this little Python program that does the job cleanly. It is released by Dr. Morrison under GPL. All yours.
#!/usr/bin/python from sys import argv from cgi import escape inFileName = argv[1] outFileName = inFileName + ".out" inFilePipe = open(inFileName, "r") outFilePipe = open(outFileName, "w") text = inFilePipe.readlines() for line in text: line = escape(line) outFilePipe.write(line) outFilePipe.close() inFilePipe.close()
Place it in your bin directory in a file named htmlify.py and give it execute permission as you did for cleanScript.sh. The usage is simple. Just run it as follows. Here we run it on a file named bar.
$ htmlify.py bar $ ls (files) bar.out (more files)
You may now read bar.out into your HTML page and display it on your all-important website.