Sample of What You'll See Using the Unix script Command
script is a standard Unix command that records a script of your interaction with the Unix system. Once it's started, it works "in the background", meaning that you continue to work normally, but the script session is dumping everything that shows up on your screen (more or less*) into some file. To start a script session, issue the command script to the Unix shell; then continue on working normally as long as you like. If you don't provide a file name to the script command, it places its output in a default file named typescript, but for CS125, I recommend you name your script file hwn.txt, where n is the number of the programming assignment you're doing.
- Whatever you do, do not use the name of your program's source code file as the filename for the output of the script command. If you type script hello.c the output from the script command will overwrite and destroy whatever used to be in hello.c Oops, there went 10 hours worth of your work. (Yes, people actually do this, usually at least one a semester.) You can't get it back; there's no trash can or recycle bin in Linux; take CS420 to learn why. See ohnosecond.Anyway, when you decide you don't need to record stuff anymore, exit from the scripting session by issuing the command exit to the Unix shell. Here's a picture and some further discussion:
Line : The % is the standard Unix shell prompt, in response to which I have entered the Unix command script and told it (script) to place all its output in a file named test_script.txt Line : This line is from the script program telling me that it has started and is recording everything. Line : This line starts with another standard Unix prompt (%), to which, in this example, I responded by entering the command ls. (When you use script to prepare a file to turn in for CS125, you won't be using ls, just cat and gcc; but Unix and the script command don't care what you do inside a script session.) Line : This is the output from the ls command. Line : This line starts with another Unix prompt, to which I reply exit, meaning "exit from the scripting session". Technically, exit exits the current shell, which you hope is actually a subshell (running script) of your login shell. If you forget where you are (you can be 20 shells deep as far as Linux is concerned; it doesn't care) and inadvertently exit from your login shell, you are logged off the system. What, you say? That's not very user-friendly? Welcome to the real world; Linux is not a toy. Line : Lines 6 and 7 are the response from script as it exits and stops the scripting session. Line : This is the standard Unix shell prompt, waiting for me to continue on, but from now on, nothing will be recorded anywhere (since I'm not in a script session anymore)
If I wanted to, at this point (line 8 and forever after) I could use an editor or other Unix tools to take a look at the output file of the script (which I put in test_script.txt, in the example above; but I could have named the output file anything I wanted or left it blank, in which case Unix would have assigned a default name of typescript). In any event, what I would see would be a perfectly ordinary text file, containing, in this particular case, lines 2 through 7, above.
* The reason for the "more or less" comment is that only characters that are input or output "normally" (i.e., a line at a time) are guaranteed to show up in the output of the script. Editors like pico, for example, do their I/O a little differently; they have to, since they use the whole screen not just a line at a time. I just ran an experiment and started pico inside a script. It looks like the script file contains a pretty nice capture of what my pico-screen looked like right at the point when I exited from pico, but there's no record of how it got to look that way (i.e., there's no real record of the keystrokes I typed into pico itself, unless they showed up visually in the final window). Running "more" from within a script session yielded strange results, however. But "normal", one line at a time, character-based input and output gets captured perfectly. Note that script captures all "normal" keystrokes, including the keys (like 'backspace' or 'delete') that you use to correct mistakes. So you'll usually see a few weird characters, like ^M and stuff like that in your script file. (You'd need to understand your keyboard map to know what these characters mean; but that's way beyond CS125.)
This page last changed 12 Oct 2003 by Dr. M.S. Jaffe