I am a newbie to unix scripting, I want to do following and I have little clue how to proceed.
I want to log the input and output of certain set of commands, given on the terminal, to a trace file. I should be able to switch it on and off.
E.g.
switch trace on
user:echo Hello World
user:Hello World
switch trace off
Then the trace log file, e.g. trace.log, it's content should be
echo Hello World
Hello World
开发者_运维知识库
One thing that I can think to do is to use set -x
, redirecting its output to some file, but couldn't find a way to do that. I did man set
, or man -x
but I found no entry. Maybe I am being too naive, but some guidance will be very helpful.
I am using bash shell.
See script
(1), "make typescript of terminal session". To start a new transcript in file xyz: script xyz
. To add on to an existing transcript in file xyz: script -a xyz
.
There will be a few overhead lines, like Script started on ...
and Script done on ...
which you could use awk or sed to filter out on printout. The -t switch allows a realtime playback.
I think there might have been a recent question regarding how to display a transcript in less
, and although I can't find it, this question and this one address some of the same issues of viewing a file that contains control characters. (Captured transcripts often contain ANSI control sequences and usually contain Returns as well as Linefeeds.)
Update 1 A Perl program script-declutter is available to remove special characters from script logs.
The program is about 45 lines of code found near the middle of the link. Save those lines of code in a file called script-declutter
, in a subdirectory that's on your PATH (for example, $HOME/bin
if that's on your search path, else (eg) /usr/local/bin
) and make the file executable. After that, a command like
script-declutter typescript > out
will remove most special characters from file typescript
,
while directing the result to file out
.
精彩评论