|
|
August
25, 2003 -
Capturing
a Session's Input and Output - The 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 operating
system command used to handle this task is
the the 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 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.
NOTE: If you are interested in adding
your login name and hostname to your command
line prompt (as seen in the above example),
check out Changing
your Command Prompt (the PS1 shell variable).
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
Our innovative hands-on training model
allows you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
 |
 |
| |
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|
|