Colors with unix command “watch”?

自古美人都是妖i 提交于 2019-11-28 16:55:46
Dennis Williamson

Some newer versions of watch now support color.

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

Related.

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.

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.

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

$ 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 tr command into this script which strips control characters, and then force the user to use --color to disable that logic. For the time being, the sheer naivete of this implementation is keeping the color-eating monster away.

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

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=129334 where they answer, that you should update the procps package

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

tl;dr

update procps

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.

Though, I am not sure how to use it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!