2

I am creating a terminal application that writes stuff to the terminal. And in order to test it, I really should test what's actually displayed in the terminal. Is there a way to read the data that's actually displayed in the terminal somehow?

The terminal must be some file somewhere or something, so there must be some way to read what's there.

cat /dev/tty

seems to give me a stream of data that's in there..But I don't want to see key strokes, just what I have written.

  • 1
    Please tell us whether or not this application is a terminal-based quasi-GUI that moves the cursor all over and maybe has pull-down menus like mc or elinks, or whether it it just prints streaming output to STDOUT and STDERR. – agc Jul 13 '19 at 20:45

3 Answers3

3

A few terminal programs provide screen dumps (such as xterm), but most do not (see Existing command line text on screen to file? (non-graphical Linux) for instance). You could make a screenshot using a tool such as gimp, but that will not tell you what the actual characters are. For development, I need to know that.

I'd use script to record the output into typescript. You can cat it back to the screen (or use less -R, if the escape sequences are simple enough). For more complicated things or long traces, I use other tools:

  • slowcat to slow down the cat progress
  • vile-pager to filter the escapes into readable form (better than less — color sequences work across lines, but still limited to single-line cursor-movement). That's a filter using vi-like-emacs, via a special-purpose utility.

To illustrate the difference, here is a screenshot of less -r on a typescript file from running apt-get update on one of my Debian machines:

enter image description here

In contrast, vile-pager highlights the places where the text was overwritten:

enter image description here

Either way (less or vile), if your application moves the cursor around the screen, the only way to review the output would be to use cat/slowcat/etc.

Thomas Dickey
  • 76,765
0

One option is to use the tee tool before all the commands you want to track. This way, you can store all your output in a selected log file.

See the example below where I save all the bash output to a text file called log.txt:

bash | tee log.txt
echo 'hello world'
ls
exit
cat log.txt

You can exit the new BASH session with a CTRL+D or simply type exit (as shown in the example above) when you're done.

If you are only looking for the commands actually passed to the terminal (it wasn't clear to me if this was what was desired by the original question), you can always just run history or check ~/.bash_history

  • hmmm interesting yeah I get the stdin and stdout duplicated, so I get "hello world" twice in the log.txt file. How does this work exactly? –  Jul 13 '19 at 04:01
  • Because "cat log.txt" is still ran within the same bash session. In other words, it's recording the fact that you're looking at the log.txt into the log.txt, which is why it's duplicated. I adjusted the answer so that you can see where it came from. – Jason K Lai Jul 13 '19 at 15:22
-3
PARAMETER_EMMC=10
while [ $PARAMETER_EMMC -eq 10 ]
    do
    read -u $USBFPI -r -n 3000 -t .3  text
        if [[ $text == *"text read from USB"* ]];  then

Execute 
fi 
done
nezabudka
  • 2,428
  • 6
  • 15
Manu
  • 1