如何使watch命令解释vt100序列?

时间:2021-09-26 06:31:13

Consider this simple example (which displays in red):

echo -e "\033[31mHello World\033[0m"

考虑这个简单的例子(以红色显示):echo -e“\ 033 [31mHello World \ 033 [0m”

It displays on the terminal correctly in red. Now consider:

watch echo -e "\033[31mHello World\033[0m"

它在终端上正确显示为红色。现在考虑:观看回声-e“\ 033 [31mHello World \ 033 [0m”

It does not display the color.

它不显示颜色。

Note: I am aware that it is easy to write a loop that mimics the basic behavior by clearing and rerunning. However, the clear operation causes the screen to flash, which does not happen under watch

注意:我知道通过清除和重新运行来编写一个模仿基本行为的循环很容易。但是,清除操作会导致屏幕闪烁,这在手表下不会发生

EDIT: Originally this question specified escape sequences rather than vt100 sequences, but that is not really what I am after, and was solved with single quotes.

编辑:最初这个问题指定转义序列而不是vt100序列,但这不是我所追求的,并用单引号解决。

3 个解决方案

#1


3  

Edit:

编辑:

More recent versions of watch support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:

更新版本的手表支持颜色。在问题示例的特定情况下,您将需要使用额外的引用级别来保留引号和转义:

watch 'echo -e "\033[31mHello World\033[0m"'

From man watch:

从男士手表:

  -c, --color
          Interpret ANSI color sequences.

Previously:

先前:

From man watch:

从男士手表:

Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.

从程序输出中删除非打印字符。如果要查看它们,请使用“cat -v”作为命令管道的一部分。

But they don't get interpreted, so I don't think there's any way.

但他们没有被解释,所以我认为没有办法。

#2


12  

From man watch of watch 0.3.0 on Ubuntu 11.10:

从Ubuntu 11.10手表0.3.0的男士手表:

By default watch will normally not pass escape characters, however if you use the --c or --color option, then watch will interpret ANSI color sequences for the foreground.

默认情况下,watch通常不会传递转义字符,但是如果使用--c或--color选项,则watch将解释前景的ANSI颜色序列。

It doesn't seem to work with your literal string on my terminal, but these work:

它似乎不适用于我的终端上的文字字符串,但这些工作:

watch --color 'tput setaf 1; echo foo'
watch --color ls -l --color

#3


1  

You can try single quoting your command :

您可以尝试单引用命令:

watch 'echo -e "\tHello World"'

On my machine this leaves me with a -e as first character, and a correctly tabbed hello world. It seems -e is the default for my version of echo. Still, it is a progress toward a correctly tabbed hello world

在我的机器上,这给我留下了一个-e作为第一个角色,以及一个正确的标签式hello世界。看来-e是我的echo版本的默认值。尽管如此,这是向正确标记的hello世界的进步

What happens is a double unquoting :
what watch see

会发生什么是双重不加引用:观看的是什么

echo -e "\033[31mHello World\033[0m"

what the shell called by watch see :

shell调用的shell看到了什么:

echo -e \033[31mHello World\033[0m

And then the backslash come into play, even when quoted, and it becomes a quoting nightmare.

然后反斜杠发挥作用,即使被引用,它也成了引用的噩梦。

#1


3  

Edit:

编辑:

More recent versions of watch support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:

更新版本的手表支持颜色。在问题示例的特定情况下,您将需要使用额外的引用级别来保留引号和转义:

watch 'echo -e "\033[31mHello World\033[0m"'

From man watch:

从男士手表:

  -c, --color
          Interpret ANSI color sequences.

Previously:

先前:

From man watch:

从男士手表:

Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.

从程序输出中删除非打印字符。如果要查看它们,请使用“cat -v”作为命令管道的一部分。

But they don't get interpreted, so I don't think there's any way.

但他们没有被解释,所以我认为没有办法。

#2


12  

From man watch of watch 0.3.0 on Ubuntu 11.10:

从Ubuntu 11.10手表0.3.0的男士手表:

By default watch will normally not pass escape characters, however if you use the --c or --color option, then watch will interpret ANSI color sequences for the foreground.

默认情况下,watch通常不会传递转义字符,但是如果使用--c或--color选项,则watch将解释前景的ANSI颜色序列。

It doesn't seem to work with your literal string on my terminal, but these work:

它似乎不适用于我的终端上的文字字符串,但这些工作:

watch --color 'tput setaf 1; echo foo'
watch --color ls -l --color

#3


1  

You can try single quoting your command :

您可以尝试单引用命令:

watch 'echo -e "\tHello World"'

On my machine this leaves me with a -e as first character, and a correctly tabbed hello world. It seems -e is the default for my version of echo. Still, it is a progress toward a correctly tabbed hello world

在我的机器上,这给我留下了一个-e作为第一个角色,以及一个正确的标签式hello世界。看来-e是我的echo版本的默认值。尽管如此,这是向正确标记的hello世界的进步

What happens is a double unquoting :
what watch see

会发生什么是双重不加引用:观看的是什么

echo -e "\033[31mHello World\033[0m"

what the shell called by watch see :

shell调用的shell看到了什么:

echo -e \033[31mHello World\033[0m

And then the backslash come into play, even when quoted, and it becomes a quoting nightmare.

然后反斜杠发挥作用,即使被引用,它也成了引用的噩梦。