Script an Interactive Session

the Unix Script Tool for Beginners


For many of the programs and excercises you will do, we want to see details of your on-line interactions with the program or with a tutorial etc. For example, we would like to see both the program and the results of running the program. The simplest way for you to do this is to use the unix script command:

Synopsis:

script scriptfile

or

script -a scriptfile

Where scriptfile is the name of the file you want script to create. Of course you can use a file name other than scriptfile. The -a is an option that tells the script command that you wish to append a new script to the file already existing scriptfile. If you don't use -a and scriptfile already exists, then the new script will overwrite the existing one. That is a mistake you may wish to avoid.

Here is an example.

Lets say you've written the required C programs program1.c, program2.c ... , for an assignment and you've conveniently named the executables program1, program2, etc. if the programs are compiled and linked. Howver, you may also run C programs in the C/C++ interpreter Ch interactively without compiling and linking. You would do the following to create a script file to print out and hand in. Remember don't type the '>'.

>script scriptfile

you will get a message saying a script has started.

>cat program1.c     the cat command will print out the contents of program1.c which will also go to the scriptfile

> program1         this will run your binary program1
> program1.c       this will run your program1.c in Ch
> ch program1.c    this will also run your program1.c in Ch

at this point you can repeat the steps for the other programs until you have executed all of your programs

> exit

stop scripting
...do something else
if you want to add anything to your scriptfile use the following:

> script -a scriptfile