CLI Magic: Using script to log your sessions

68

Author: Shashank Sharma

Sometimes when working on a trivial task, such as creating a MySQL account, I find myself unable to remember the exact steps, despite having done it many times in the past few years. When such temporary amnesia strikes, you can turn to script, a command-line utility that keeps a log of your console session. It can record for future reference all the commands you use.

User Level: Beginner

You can use the command line for anything from text editing to SSH and FTP logins, and to download stuff from the Web. Script can help network admins keep an eye on what users are doing on their consoles. It’s also an efficient way for people who are writing installation notes about an application to keep track of their steps. And new users can use it to record error messages.

Launching script

Running script from your shell will display the message Script started, file is typescript and fork the shell. Any commands you then run — along with their output — will be stored in a file, named typescript by default. You can name your own log files by supplying a filename argument to script; script filename will create a file called filename in your working directory and record all subsequent commands in it.

By default, script will overwrite any data in the file if it already exists. If you already have a log file and wish to continue using it, use the -a switch. script -a filename will append new commands to the end of filename, retaining the previous contents. Use the -c switch if instead of recording the complete console session you only wish to record one command and its output.

To see how script works, run script simpletasks and try out some basic commands. Type pwd and press Enter, followed by uptime, date, whoami, and echo $SHELL. Now press Ctrl-d or type exit to terminate script and use cat to view the content of simpletasks:

$ cat simpletasks
Script started on Friday 07 April 2006 12:32:39 PM IST
$ pwd
/home/linuxlala
$ uptime
 12:33:16 up 1:04, 2 users, load average: 0.06, 0.15, 0.12
$ date
Fri Apr 7 12:33:22 IST 2006
$ whoami
linuxlala
$ echo $SHELL
/bin/bash
$
Script done on Friday 07 April 2006 12:34:40 PM IST

When you run it, the first thing script records is the start time. The last line in the log file indicates when script was terminated.

Cleaning garbage

Script records even the newline, backspace, and similar characters to the log file. When you try to view the log files script creates using vi, you will see a lot of these control characters, which look like garbage or junk characters in the file. While gedit is smart enough to recognize ^M as carriage-return, vi cannot handle it, and thus each line of the log file in vi ends with ^M.

Similarly, ^[ is an escape character and ^H is backspace. Wherever you see ^H in your log file, it means that the letter preceding it was erased and should not be there.

cd ..^H^[[K^H^[[Klinuxlala/^M means that after typing cd .., I removed the two dots. When I delete the first dot using the backspace key, it results in the ^H character. Next is the ^[ character followed by a [K, which denotes a change in the cursor position. Then again comes the backspace, escape, and cursor shift characters, because I deleted the other dot also.

You can clear out this garbage using sed. A simple search and replace string will take care of all the ^M characters.

In vi, the command :.,$s/old/new/g replaces all occurrences of old with new. Pressing escape : takes you into vi’s command mode. The .,$ determines the range of the search operation: the dot means start on the current line, and the dollar sign means end at the end of the line. The s invokes sed. The g at the end of the command ensures that the changes are applied globally. Therefore, :.,$s/^M//g removes all occurrences of ^M.

Note that ^M is not a caret character followed by the letter M. It is a control character, and to write it you need to press the control key and then press ‘v’ and ‘m.’ Ctrl-v-m will result in the carriage return character. Similarly, you can produce the escape character by pressing Ctrl-v-[.

The inclusion of control characters in script’s logs may put off some people, but as long as you use cat to view the file, the control characters won’t bother you. In any case, the simple search-and-replace trick can help you do away with all the ugly control characters.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.