3

I tried to log bash session using:

js:/var/tmp # script -aqf /var/tmp/out

In out file I see, besides output, a lot of garbage. Instead of just whitespaces and new lines I see this, for example:

Script started on Tue Jul 14 14:17:57 2015
^[[?1034h^[[1m^[[31mjs-om:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[mpwd^M
/var/tmp^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[m^M
^[[1m^[[31mjs:/var/tmp # ^[(B^[[mexit^M
exit^M

I think the problem is with encodings. Is there a solution for this issue?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Yuriy Vasylenko
  • 141
  • 1
  • 7
  • If you just want to replay in your terminal the session : more -v thescriptfile. otherwise you'll need, as said below, to avoid your shell (... and commands) to create those escape codes, or find a proper sed to get rid of them (very complex afterwards, as some escape codes look widely different than the ones you show, and thus could take out huge chunks of the real output if you only have regexp trying to get rid of those ones) – Olivier Dulac Jul 15 '15 at 00:50
  • It seems that with doing cat or more of script_file and manual coppying of text I can get session log without special characters. Of course it is not good to do like this permanently, but as a quick workaround it works – Yuriy Vasylenko Jul 15 '15 at 12:24
  • the easy way for the latter: open a putty session to the machine, cat the file, and right-click "copy all to clipboard". then paste into notepad (in case you added the usually usefull "copy also also the rtf in clipboard" option, pasting in notepad strips that out. the option is in other cases usefull in keeping some color info when pasting to word or similar) – Olivier Dulac Jul 15 '15 at 12:28
  • thanks, meanwhile where did you get -v option for more command ? – Yuriy Vasylenko Jul 15 '15 at 12:34
  • I always : man somecommand, before I use any command. Especially as similarly named commands have wildly different options on different systems (and sometime completely different actions too, such as "reset" which can either clear the terminal or reboot the host, depending on which system you are loggued into...) – Olivier Dulac Jul 15 '15 at 12:36
  • Well, ok. I checked man more once again. There is no -v option for more in linux. Only v command which allows you to open editor from more. – Yuriy Vasylenko Jul 15 '15 at 12:52

3 Answers3

2

No the problem is not with encoding. You don't seem to follow the prerequisites for screen as mentioned in the man page:

 Certain interactive commands, such as vi(1), create garbage in the type‐
 script file.  Script works best with commands that do not manipulate the
 screen, the results are meant to emulate a hardcopy terminal.

You have all kind of stuff probably resulting from a complex prompt. Set your prompt

PS1="$ " 

to prevent that from writing garbage.

Anthon
  • 79,293
  • This excerpt from man page has nothing to do with prompt format. It means that using commands like vi, top etc(which manipulate display) will result in garbage, but not the format of your prompt. I see, it is obvious that with $ prompt there will be less control characters in log, but this is just workaround which is not suitable for me at all. And yes, now I see that this is not encoding issue. It's terminal control characters(incl color whitespaces etc) which might be deleted via script – Yuriy Vasylenko Jul 14 '15 at 12:53
  • Here is an example from my log ( just output from command ) : /var/tmp^M – Yuriy Vasylenko Jul 14 '15 at 12:56
  • That changing your prompt is not a solution is unclear from your question. Garbage in means garbage out and understanding where the garbage comes from make much more sense than blindly stripping jung from a file if you don't know where that junk comes from and/or if you will filter future versions. I would prefer the mess from happening in the first place. – Anthon Jul 14 '15 at 12:57
  • Changing promt is not a deep dive into problem as well. It is even not a workaround as it leaves staff like /var/tmp^M. I use terminal, wich as I understand, require control characters to present session well(colors etc). And bash(I guess but I don't now for sure right now) appends those characters, and they a logged with payload. If you now how to leave those characters for terminal, and avoid them when writing to log, than it is good solution. Now I see only your half-working workaround and script. – Yuriy Vasylenko Jul 14 '15 at 13:11
1

As well as fixing up your PS1, you may be able to persuade applications not to emit these non-printing characters by exporting a suitable value in the TERM environment variable. The canonical one is probably export TERM=dumb at the beginning of your script session (or TERM=dumb script <args> when invoking). Assuming that you create your PS1 portably using tput, the latter may well work for that, too.

Toby Speight
  • 8,678
0

Removing control chars (including console codes / colours) from script output appears to answer your question.

First you use script + after cleanse the typescript file of all escape sequences.