share
Unix & LinuxRun line editing for user input (script command)
[+1] [1] André Werlang
[2020-04-14 19:12:33]
[ shell line-editor ]
[ https://unix.stackexchange.com/questions/580073/run-line-editing-for-user-input-script-command ]

I used the script program to record an interactive session:

$ script
$ echo<-<-<-<-printf "hello\n"
$ exit

The sequence <- is representing the backspace character.

I want the following as output:

$ printf "hello\n"
$ exit

But the actual result contains all individual keystrokes (looking with cat --show-nonprinting).

I tried reading line-by-line with the read command. It does edit out the backspaces, but it also removes the command prompt, and it seems it converts \n into n.

I'm looking for a way to have the text the user was seeing on screen (command prompt, edited input and command output -- colors don't matter).


For reference a sample output:

^[[?2004l^M^[[?2004h$ echo^H^[[K^H^[[K^H^[[K^H^[[Kprintf "hello\n"^M
^[[?2004l^Mhello^M
^[[?2004h$ exit^M
^[[?2004l^Mexit^M
Will scriptreplay help? Those strings (ESC [ digits ; digits m) are "VT100 Control Sequences", and are influenced by the $TERM environment variable. I've used an Emacs function to eliminate them from my script logs, but suggesting "Learn Emacs" is extreme overkill. - waltinator
@waltinator no, the output is virtually the same with scriptreplay - André Werlang
From your reference sample, you seem to have quite complex a prompt including exotic (?) console codes, and there are several control characters missing, like ^M (0x0D, <CR>), ^H (0x08, <BS>), and ^[ (0x1B; <ESC>). Why not start debugging with simpler, straight forward examples? And, why wouldn't cat file | while read -r ... work? - RudiC
@RudiCcorrect, I have a custom PS1, will try with a simpler PS1 and see how that goes. I recall seeing ^M, ^H, not sure why it's missing in the sample. - André Werlang
@RudiC replaced the sample with PS1="$ ", ^H, ^M are there - André Werlang
[0] [2020-04-15 17:53:10] Arkadiusz Drabczyk

IIUC, ansi2txt [1] will do exactly what you want:

$ ansi2txt typescript > ANSI2TXT
$ cat ANSI2TXT
$ printf "hello\n"
hello
$ exit

Script done on Wed 15 Apr 2020 07:48:55 PM CEST
[1] https://sourceforge.net/projects/ansi2txt

it looks promising, since it prints the command prompt as well and the escaped \n - André Werlang
1