"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 II
Last
week's tip demonstrated how to capture a session’s input
and output using the UNIX 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.
New to sed? Read A Brief Introduction to sed.
Read the PREV article in this series -
Capturing a
Session's Input and Output - The UNIX script Command - Part I