"Taking a LiveFire Labs' course is an excellent way to learn
Linux/Unix. The lessons are well thought out, the material is
explained thoroughly, and you get to perform exercises on a real
Linux/Unix box. It was money well spent."
Ray S.
Pembrook Pines, Florida
LiveFire Labs' UNIX and Linux Operating System Fundamentals
course was very enjoyable. Although I regularly used UNIX systems
for 16 years, I haven't done so since 2000. This course was a
great refresher. The exercises were fun and helped me gain a real
feel for working with UNIX/Linux OS. Thanks very much!"
Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada
Read
more student testimonials...
Receive UNIX Tips, Tricks, and Shell Scripts by Email
LiveFire Labs' UNIX Tip,
Trick, or Shell Script of the Week
Capturing a Session's Input and Output - The UNIX script Command -
Part I
There are many reasons why a person using UNIX or Linux would
want to capture a session's input and output, or in other words log
everything that is printed to the screen (including prompts). A
general user may want to retain a sequence of commands for future
reference, a programmer may want to capture compiler warning and error
messages for a post-compile review, or a system administrator may want
to log the steps taken to install a piece of system software in case
something goes wrong or if they have to document or repeat the correct
installation procedure.
Regardless of the reason, the command used to handle this task is the
the UNIX script command. Running this command will fork a new shell
(which shell to use is determined by looking at $SHELL), and will
terminate when the new shell is exited with "exit" or CTRL-D. A
message similar to this:
Script started, file is unixfile
is printed when the script is started, and the ending message will be:
Script done, file is unixfile
The syntax for the UNIX script command is:
script [filename]
If a filename is omitted, the default name of "typescript" will be
used. The following is an example of how to use script to capture the
execution of a few frequently used UNIX commands:
[root@hawk] # script unixfile
Script started, file is unixfile
[root@hawk] # date
Sat Aug 23 20:14:46 CDT 2003
[root@hawk] # uptime
8:14pm up 3:21, 1 user, load average: 0.00, 0.00, 0.01
[root@hawk] # pwd
/tmp
[root@hawk] # hostname
hawk
[root@hawk] # who am i
root /dev/pts/1 Aug 23 20:15
[root@hawk] # exit
Script done, file is unixfile
[root@hawk] #
Now let's display the contents of the script file...
[root@hawk] # cat unixfile
Script started on Sat 23 Aug 2003 08:14:43 PM CDT
[root@hawk] # date
Sat Aug 23 20:14:46 CDT 2003
[root@hawk] # uptime
8:14pm up 3:21, 1 user, load average: 0.00, 0.00, 0.01
[root@hawk] # pwd
/tmp
[root@hawk] # hostname
hawk
[root@hawk] # who am i
root /dev/pts/1 Aug 23 20:15
[root@hawk] # exit
script done on Sat 23 Aug 2003 08:15:09 PM CDT
[root@hawk] #
If you try this example on your own system and then vi the script
file, you will notice that script also captured control characters
(e.g. ^M) in the file. Next week's tip will present a few options for
quickly removing these characters from the file.
Read the NEXT article in this series -
Capturing a
Session's Input and Output - The UNIX script Command - Part II