September
1, 2003 -
Capturing
a Session's Input and Output - The script
Command - Part II
|
|
The
previous tip demonstrated how to
capture a session’s input and output
using the script command.
If you tried the example from last
week on your own system, you probably
noticed that script also captured control
characters (e.g. ^M) in the file.
This can be seen by viewing the file using
the vi text editor:
|
Script started on Sat 30 Aug 2003 02:06:10 PM CDT
[root@hawk] # date^M
Sat Aug 30 14:06:31 CDT 2003^M
[root@hawk] # uptime^M
2:06pm up 4 min(s), 1 user, load average: 0.07, 0.21, 0.11^M
[root@hawk] # pwd^M
/tmp^M
[root@hawk] # hostname^M
hawk^M
[root@hawk] # who am i^M
root /dev/pts/1 Aug 30 14:06^M
[root@hawk] # exit^M
script done on Sat 30 Aug 2003 02:06:53 PM CDT
|
This week's tip will present four options for removing the ^M (control character) from the script
file.
The first option, which is also the least desirable, is to
manually edit the file. This may be fine for
a small file like the one used in our example, but what if you had thousands of lines with
^M at the end of them. Not a very
appealing option, is it?
The second option is a more palatable solution, but still not the optimal one. The sed command
could be used to strip the last character from each line:
|
[root@hawk] # sed s/.$//g unixfile > unixfile.sed
|
Unfortunately, this also removes characters that you may not want removed (e.g. the "T" in
"CDT"):
|
Script started on Sat 30 Aug 2003 02:06:10 PM CD
[root@hawk] # date
Sat Aug 30 14:06:31 CDT 2003
[root@hawk] # uptime
2:06pm up 4 min(s), 1 user, load average: 0.07, 0.21, 0.11
[root@hawk] # pwd
/tmp
[root@hawk] # hostname
hawk
[root@hawk] # who am i
root /dev/pts/1 Aug 30 14:06
[root@hawk] # exit
script done on Sat 30 Aug 2003 02:06:53 PM CD
|
The third option uses sed again, but strips the specific
character instead of the last character on each line:
|
[root@hawk] # sed s/^M//g unixfile > unixfile.sed2
|
One very important item to understand about this
command is that the "^M" (control character)
is not generated by typing the "^" character, and then the
"M" character from your keyboard. Instead, it is accomplished by typing Ctrl-V and then Ctrl-M (the Ctrl key and the V or M key
are pressed simultaneously). Typing this sequence will produce the "^M" (control
character), which allows sed to locate and process it as
instructed.
The most desirable option in my opinion, is running the
dos2unix utility against the script file:
|
[root@hawk] # dos2unix unixfile unixfile.dos2unix
|
|
This command will convert a text file from DOS format to ISO format, but may not be available
on your system. Its availability is dependent on what operating system you
are using.
|
NOTE: New to sed? See A
Brief Introduction to sed.
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 UNIX online...
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
|
|