Linux 带有 unix 命令“watch”的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3793126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:17:15  来源:igfitidea点击:

Colors with unix command "watch"?

linuxbashubuntuwatch

提问by never_had_a_name

Some commands that I use display colors, but when I use them with watch the colors disappears:

我使用显示颜色的一些命令,但是当我将它们与 watch 一起使用时,颜色消失了:

watch -n 1 node file.js

Is it possible to have the colors back on somehow?

是否有可能以某种方式恢复颜色?

采纳答案by Paused until further notice.

Some newer versions of watchnow support color.

一些较新的版本watch现在支持颜色。

For example watch --color ls -ahl --color.

例如watch --color ls -ahl --color

Related.

相关的

回答by Benoit

From watch manual:

从手表手册:

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”作为命令管道的一部分。

Though, I am not sure how to use it.

虽然,我不确定如何使用它。

回答by davidDavidson

YES

是的

watch works with color output. it is part of the procps package (at least in debian) here is bugreport for your question http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=129334where they answer, that you should update the procps package

手表与颜色输出一起使用。它是 procps 包的一部分(至少在 debian 中)这里是您的问题的错误报告http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=129334他们回答,您应该更新 procps包裹

e.g. with ubuntu 11.04 this package works http://packages.debian.org/wheezy/procps

例如在 ubuntu 11.04 这个包工作http://packages.debian.org/wheezy/procps

tl;dr

tl;博士

update procps

更新过程

回答by Kaz

You can duplicate the fundamental, no-frills operation of watchin a couple lines of shell script.

您可以watch在几行 shell 脚本中复制基本的、简洁的操作。

$ cat cheapwatch 
#!/bin/sh

# Not quite your Rolex

while true ; do
  clear
  printf "[%s] Output of %s:\n" "$(date)" "$*"
  # "$@" <- we don't want to do it this way, just this:
  ${SHELL-/bin/sh} -c "$*"
  sleep 1  # genuine Quartz movement
done

$ ./cheapwatch ls --color  # no problem

Eventually, someone very clever will hack a trcommand into this script which strips control characters, and then force the user to use --colorto disable that logic. For the time being, the sheer naivete of this implementation is keeping the color-eating monster away.

最终,一个非常聪明的人会tr在这个脚本中加入一个命令来去除控制字符,然后强制用户使用--color禁用该逻辑。就目前而言,这种实现的纯粹天真使吃颜色的怪物远离。

If you're in a situation where watchdoesn't have the --coloroption and you can't upgrade the package for whatever reason, maybe you can throw this in.

如果您处于watch没有--color选项并且由于任何原因无法升级软件包的情况,也许您可​​以将其放入。

回答by theist

Do not use watch... When you use watch programs can detect they're not writing to a terminal and then strip the color. You must use specific program flags to keep the control codes there.

不要使用watch......当您使用手表程序时,可以检测到它们没有写入终端,然后去除颜色。您必须使用特定的程序标志来保存控制代码。

If you don't know the flags or there isn't you can make a poor's man watch by:

如果您不知道旗帜或没有旗帜,您可以通过以下方式让穷人观看:

while sleep <time>; do clear; <command>; done

It will have a bit of flicker (watch works "double buffered") but for some stuff it is useful enough.

它会有一点闪烁(手表工作“双缓冲”)但对于某些东西它已经足够有用了。

You may be tempted to make a double buffered poor man's watch using

您可能会想使用以下方法制作双缓冲穷人手表

while sleep <time>; do <command> > /tmp/file; clear; cat /tmp/file; done

But then you'll hit again the "I am not writing to a terminal" feature.

但是你会再次点击“我没有写到终端”功能。

回答by lol

While other answers solve this problem, the easiest way to accomplish this is using the unbuffertool. To use it simply do:

虽然其他答案解决了这个问题,但最简单的方法是使用该unbuffer工具。要使用它,只需执行以下操作:

$ watch --color 'unbuffer <your-program>'

This way you don't have to hunt for control sequence enabling flags of your program. The caveat however is that your version of watch should support the --colorflag.

这样您就不必寻找控制序列启用程序的标志。但是需要注意的是,您的手表版本应该支持该--color标志。

You can install unbuffer on Debian or Ubuntu using sudo apt-get install expect.

您可以在 Debian 或 Ubuntu 上使用sudo apt-get install expect.