home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

37.8. Cleaning script Files

As Section 37.7 explains, the files made by the script program can have stray control characters in them. The shell script called script.tidy can clean them up. Dan Bernstein wrote it and posted it to Usenet; I made a few changes. It reads from files or standard input and writes to standard output.

Figure Go to http://examples.oreilly.com/upt3 for more information on: script.tidy

script.tidy uses the sed (Section 34.1) substitute command to remove CTRL-m (RETURN) characters from the ends of lines. It uses the sed test command (Section 34.21) to repeat a series of commands that delete a character followed by CTRL-h (BACKSPACE). If you use DELETE as your erase character (Section 5.8), change the script to eat DELETE instead of BACKSPACE. script.tidy uses a trick with echo and tr to store the control characters in shell variables. Because the sed script has doublequotes (Section 27.12) around it, the shell variables are substituted in the right places before the shell starts sed.

eval Section 27.8, exec Section 36.5

#!/bin/sh

# Public domain.

# Put CTRL-M in $m and CTRL-H in $b.
# Change \010 to \177 if you use DEL for erasing.
eval `echo m=M b=H | tr 'MH' '\015\010'`
exec sed "s/$m\$//
:x
s/[^$b]$b//
t x" $*

You can also hack the sed script in script.tidy to delete some of your terminal's escape sequences. (A really automated script.tidy would read your termcap or terminfo entry and look for all those escape sequences in the script file.)

Bear in mind that script was designed to emulate a paper terminal; if you've modified your prompt, especially if you are using multiple-line prompts, your script output is going to be full of far worse junk than script.tidy can fix. If you find that script simply doesn't do it for you, you should consider whether you want a complete record of all terminal input and output or just a record of what you typed. If the latter is more to your liking, you should look into the various history editing and printing capabilities provided by modern shells.

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.