Pregunta

http://www.termsys.demon.co.uk/vtansi.htm this link has a bunch of escape codes, and I've written scripts that have moved the cursor and changed the color - I'm curious how one goes about programatically getting responses? It doesn't seem to be standard out or standard error as far as I can tell so I am very confused

For example in ZSH I do the following:

jessed@joriet-mac ~ » echo "\e[6n"

jessed@joriet-mac ~ » 3;1R

the response to the query comes through as the next Terminal command? I don't understand why, also Bash doesn't seem to demonstrate this behavior.

I know there are other ways to do this using tput for example, but I'd like to know on a lower level what was going on and why.

¿Fue útil?

Solución

The responses come through on the channel from the terminal (or terminal emulator) to the serial port (or other tty device). That's the same channel used for transmitting characters entered at the terminal keyboard; there is no out-of-band signaling.

Since you didn't read the response after sending the query, it was interpreted as a series of keypresses by your shell. The different shells have different responses to the unusual keyboard input.

To read the response properly, you have to take the terminal out of line-based ("icanon" or "cooked") mode and read a byte at a time (from the tty, i.e. possibly stdin, the same place you'd read keyboard input from) until the terminating character is found. And there's no real way to distinguish the response from any real keypresses that happened to occur at the same time.

It's an unclean business, and if you're trying to do it in a shell script you add extra pain.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top