Alan D. Salewski on 4 Sep 2011 07:10:48 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Capturing command line, and stdio and error with bash


On Sun, Sep 04, 2011 at 08:35:57AM -0400, Edmond Rodriguez spake thus:
> Well there has been lots of mail about redirection in bash.
*snip*

> So I would want a script that has only the final result of my command
> entered (like history does) I entered, not the editing keystrokes as
> well (back space, emacs or vi control keys....cursor movements), and
> of course whatever else gets output to the terminal.    I realize if a
> formatting strings goes to the terminal from an *application* I am
> running,  I will get that, but I am just thinking of general basic
> text i/o going to the terminal from an application.
> 
> Edmond

Here you go:

I started with the perl program found here:

    http://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output/18979#18979

The text of that is in the 'strip-em' program below. By itself, that
script could be used to post-process a file created by 'script' (or
similar). It tries to "remove carriage returns and do backspace-erasing
of previous characters", and in my simple test it seemed to do all
right.

But since that's not quite what you're looking for, and because we're
all on a "redirection in bash" kick, I present the following
'script-filt' program. It sets up a named pipe, and then fires up a
'strip-em' process in the background to process whatever gets written to
the pipe. It then launches 'script' and tells it to write to the named
pipe rather than the normal 'typescript' file. From there it looks like
a normal 'script' session -- only the logged output is subject to
filtering. In the end, you're left with a 'typescript-filt' file. The
filtering is not quite perfect, but it's a good start.

HTH,
-Al


----script-filt------------------------8<---------------------------------
#!/bin/bash

set -e

declare -r pname="filt-${RANDOM}"

mknod "${pname}" p

./strip-em < "${pname}" > 'typescript-filt' &

script -f "${pname}"

rm -f "${pname}"
---------------------------------------8<---------------------------------




----strip-em---------------------------8<---------------------------------
#!/usr/bin/perl
while (<>) {
    s/ \e[ #%()*+\-.\/]. |
       \r | # Remove extra carriage returns also
       (?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
       (?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
       (?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ...  ST
       \e.|[\x80-\x9f] //xg;
       1 while s/[^\b][\b]//g;  # remove all non-backspace followed by backspace
    print;
}
---------------------------------------8<---------------------------------





-- 
-----------------------------------------------------------------
a l a n   d.   s a l e w s k i                   salewski@att.net
1024D/FA2C3588 EDFA 195F EDF1 0933 1002  6396 7C92 5CB3 FA2C 3588
-----------------------------------------------------------------
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug