Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: gnu script and zsh: only log command output



On Thu, May 12, 2022 at 11:46 AM chiasa.men <chiasa.men@xxxxxx> wrote:
>
> When I use "script --log-output o --log-input i" I am facing two problems:
>
> 1: The output log contains the actual input: e.g. ".]0;test@test: echo 5.5"
> (see below)
> 2: The prompts

This is going to happen because "script" has taken over the terminal
device and ZLE must necessarily write to that terminal to display the
prompts and to echo back your keystrokes as it maintains the "editor"
state.  Everything that comes and goes to the terminal is potentially
captured by "script" and there's no way for you to see/interact with
it that bypasses that.  You can try switching off ZLE if you can stand
to work that way, but I think you'd still get the prompts captured.

> I really just want the programs output logged - no prompts, no shell-related
> output.
>
> Is that possible?

It's a bit difficult in zsh up to 5.8.1 ... once 5.9 is released, you
should be able to do something like this:

autoload add-zsh-hook
exec {stdout}>&1 {stderr}>&2
preexec_outputs() {
  exec 2>>|stderr.log 2>&$stderr
  exec 1>>|stdout.log 1>&$stdout
}
precmd_outputs() {
  exec 1>&$stdout 2>&$stderr
}
add-zsh-hook preexec preexec_outputs
add-zsh-hook precmd precmd_outputs

However, this won't capture output of interactive programs like "vim"
which will either fail or will re-open the terminal device directly
(printing an error message first, in the case of vim).

> So would there be a way to reliably filter the zsh-related output e.g. within
> the _my_read_function function?

Using preexec and precmd you could write markers to the output that
would indicate when ZLE becomes active (precmd) and when it finishes
(preexec).  Your python script could then discard anything between
those two markers.  You'd have to be very sure that those markers
would never appear in output you actually want captured.




Messages sorted by: Reverse Date, Date, Thread, Author