3

I am on a Debian system fresh installed, logged in via ssh on /dev/pts/0, and have entered the following command

watch -n 10 clear > /dev/pts/0 ; netstat -tupn 2>/dev/null | grep -v 3306 > /dev/pts/0

and what I get looks like this

Every 10.0s: clear                                              Thu Jan 30 17:42:01 2020

^[3;J^[H^[2J

Why is this linux box hating me ???

9
  • Since watch already refreshes the display, why are you explicitly telling it to clear the screen? Actually, you are watching the clear command - not very interesting...
    – doneal24
    Commented Jan 30, 2020 at 18:02
  • @doreal no, I have there 2 things that I do each time: a clear and a netstat.
    – Max Muster
    Commented Jan 30, 2020 at 18:06
  • 2
    So you want to watch the output of clear every 10 seconds until you hit ^C. After you're done with watching clear, you want to run netstat once. And all of this is output to a specific pseudo-terminal.
    – doneal24
    Commented Jan 30, 2020 at 18:20
  • 3
    It is the "clear screen" escape sequence for ANSI terminals.However, sinnce watch is capturing input, it grabs the escape sequence and displays it. It also sends its own clear screen escape sequence to the output terminal independently of the command given.
    – doneal24
    Commented Jan 30, 2020 at 18:23
  • 1
    Running clear > /dev/pts/0 should not give you that "funny screen" on any Linux system, although the results may not be want you expect if your terminal is not /dev/pts/0. watch clear > /dev/pts/0 is an entirely different command and you should not expect the output to be the same.
    – doneal24
    Commented Jan 30, 2020 at 18:41

1 Answer 1

8

It is the output of the clear command …

You ran clear. clear produced these control sequences. (They are merely in a different order on my machine.)

% clear | cat -v ; echo
^[[2J^[[H^[[3J
% 

On an ECMA-48 terminal or terminal emulator these control sequences erase the display and position the cursor.

% clear | console-decode-ecma48
ED 2
CUP 0
ED 3
% 

But the watch command interprets the standard outputs of the processes that it runs, and does not understand any ECMA-48 control sequences apart from one. It only understands some of the TTY-37 control characters from 1968 and (if the -c flag is used) the SGR control sequence from ECMA-48.

Everything else it passes to ncurses, which prints control characters in caret notation. But because of a bug in watch, the second character of a caret notation string is then immediately overwritten, and what should have been ^[[ comes out as ^[ in watch's output.

… and only the clear command.

The shell script is not quoted at all:

watch -n 10 clear > /dev/pts/0 ; netstat -tupn 2>/dev/null | grep -v 3306 > /dev/pts/0

So this is two pipelines, sequentially:

watch -n 10 clear > /dev/pts/0
netstat -tupn 2>/dev/null | grep -v 3306 > /dev/pts/0

The redirection happens before the commands are run, and in two cases is redirecting standard output to the pseudo-terminal that it was open to anyway. They are effectively no-ops:

watch -n 10 clear 
netstat -tupn 2>/dev/null | grep -v 3306

Your machine is doing exactly what you told it to do.

So you are watching the output of just the clear command, repeatedly, whose output isn't being processed as control sequences but simply printed in broken caret notation.

Exit watch to run your netstat command once. ☺

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .