5

I'm trying to build vlc, which is rather complicated and dependent on having the right packages installed, and keeps choking on errors. To trace all my steps, I wish to output what I'm doing to a logfile as I interact with a shell.

As per the helpful answer to this question

I got an elevated prompt by typing the following

(sudo bash) | tee -a vlc_attempt.log

Unfortunately, the output does not include the path prompt, which I wish to see to tell input apart from output.

I tried piping stderr to stdout and vice versa, both of which produce awry results, but enter nothing into the file. Same thing with trying to pipe lxterminal to tee, or starting lxterminal with the --command= option and then the above example command in quotes. Where to go from here?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Rob F
  • 297
  • 4
  • 11

1 Answers1

9

Use script(1) to log everything sent to the terminal:

$ script
Script started, file is typescript
$ # do your work
...
$ # then exit with ^D
$ exit
Script done, file is typescript

You can later look at the output with less:

$ less -r typescript

Beware that the logs will contain all control characters sent to the terminal, such as ANSI colours or whatever else your shell prompt sends. If you don't want control characters in the logs then either simplify your PS1 before running script, or use something like stripansi(1) to clean up the output.

lcd047
  • 7,160
  • 1
  • 22
  • 33
  • Thank you for the answer. While that provides a solution (actually I haven't tested it yet, but I'll assume it does) I would like to know if the method I was originally going for can be made to work and would like to know why I am not seeing the user prompt in the piped output. – Rob F Jul 04 '15 at 15:26
  • 4
    @RobF It can't be made to work. `tee` is useful for logging output from a command, not interactive sessions. The only reason you get to see a prompt at all is that you aren't redirecting `stderr`. Once you redirect `stderr` you won't have a shell prompt at all. Really, use the right tool for the job. – lcd047 Jul 04 '15 at 15:31
  • @RobF no, I don't think it can. The prompt is not part of either stdout or stderr which are the only things you can redirect. This is because in the crushing majority of cases, when you redirect bash's output you _don't_ want the prompt. It would break 99% of pipes. Yours is a very special case. – terdon Jul 04 '15 at 15:31
  • Why would it break the pipes? And is the prompt not a part of any stream, it targets the terminal directly? – Rob F Jul 04 '15 at 16:03
  • @ucd047 it's not that I'm stubbornly wanting to use a knife to eat cereal, I just want to learn something extra while I'm here. – Rob F Jul 04 '15 at 16:05
  • @RobF there are two reasons to redirect streams: for manual human inspection, or for automatic parsing. In the second case, you would have to remove the prompt from the data, that is a taunting task, as it can be literally anything. – Davidmh Jul 04 '15 at 16:22