我知道在bash中我可以打印一条彩色的字符串,就像:
echo -e "\033[33;1mhello\033[0m"外壳中的输出将是金色的hello。但是,当我将输出重定向到文件test.txt时,\033[33;也会出现在文本文件中。但是,grep --color=auto命令不会将这些字符重定向到文本文件中。它怎么能做到这一点?
发布于 2013-09-02 08:14:20
它可能在stdout文件描述符(即1)上使用isatty(3)库函数。所以用吧
if (isatty(STDOUT_FILENO)) {
// enable auto colorization
}在你的C代码中。
在shell脚本中,使用tty(1)命令:
if tty -s ; then
# enable auto colorization
fi或者简单地说是-t 试验(1)
if [ -t 1 ]; then
# enable auto colorization
fi发布于 2013-09-02 08:24:54
这个怎么样?
#!/bin/bash
if [ -t 1 ]; then
echo -e "\033[33;1mhello\033[0m"
else
echo hello
fi这里的解释是:
test -t <fd>的短形式是[ -t <fd> ],它检查描述符<fd>是否是终端。来源:help test
发布于 2013-09-02 08:38:07
使用带有导出标志的GREP_COLORS变量。对此进行了测试,它起了作用:
export GREP_COLORS='ms=01;33'
grep --color=auto -e hellohttps://stackoverflow.com/questions/18568426
复制相似问题