bergman on 4 Sep 2011 16:41:36 -0700


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

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


In the message dated: Sun, 04 Sep 2011 10:50:23 EDT,
The pithy ruminations from Edmond Rodriguez on 
<Re: [PLUG] Capturing command line, and stdio and error with bash> were:
=> Yes, I thought of doing something similar, but it still does not cover
=> things like viewing history.  For example pulling up old commands.
=> You end up with all the commands in the typescript (using script),
=> though you really only entered one of them (which history records just
=> fine).  Also things like running vi or whatever, I would just want the
=> "vi xxx" to record, not everything going on in the vi session.
=> 
=> Imagine that you are doing an install or some meticulous work
=> manipulating things, and you want to record what you are doing.  What

Hmmm...sudosh will do what you want, though it does require installing
software.  Sudosh is designed as a shell for users granted permission
to use sudo. It records all input and output, and you can 'play back'
the events in real-time or speeded up.

As for filtering the results of script(1), take a look at col(1) --
the "-b" option. That won't process all terminal control sequences,
but will handle backspaces. That's a starting point.

=> commands you actually entered, and what the applications or shell
=> outputted, and that is it.      Of course, I realize there may be
=> something out there that does this, but I am trying stay in the
=> mindset of a  modern common Unix or Linux install (writing a script
=> would be fine), without requesting new software installed.
=> 
=> History does great recording commands.
=> 
=> Script of I/O redirection using tee (though pain to type every time),
=> does great for recording output.
=> 
=> How to do both in one place?
=> 
=> 
=> On Sun, Sep 4, 2011 at 10:10 AM, Alan D. Salewski <salewski@att.net> wrote:
=> > 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
=> >
=> ___________________________________________________________________________
=> 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
=> 
=> 

___________________________________________________________________________
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