在自己的bash脚本中实现--color=auto
我在网上寻找了这个问题的答案,但没有找到任何关于这个问题的答案(查找在自己的bash脚本中实现--color=auto,bash,terminal,Bash,Terminal,我在网上寻找了这个问题的答案,但没有找到任何关于这个问题的答案(查找bash和color可以得到许多其他与颜色相关的结果) 我想做的是在我自己的bash脚本中实现一个--color=(never | always | auto)参数,类似于ls、grep或其他命令中的选项。我一直不明白auto选项是如何隐藏起来的,它可以告诉我命令的输出是通过管道传输还是重定向的 我在想这样一个例子: color=... # auto | always | never ... if [[ $color == au
bash
和color
可以得到许多其他与颜色相关的结果)
我想做的是在我自己的bash脚本中实现一个--color=(never | always | auto)
参数,类似于ls
、grep
或其他命令中的选项。我一直不明白auto
选项是如何隐藏起来的,它可以告诉我命令的输出是通过管道传输还是重定向的
我在想这样一个例子:
color=... # auto | always | never
...
if [[ $color == auto ]]; then
# 0 for stdin, 1 for stdout, 2 for stderr
if [[ -t 1 ]]; then
# we're writing to a tty so use colors
...
else
# not a tty, no colors
...
fi
fi
example.sh
#!/bin/bash
text=“这是一些文本”
如果[[“$1”=”--color=始终”];然后
echo-e“\e[1;3600万${text}\e[0万”#浅蓝色
elif[“$1”==”--color=never”];然后
回显“$text”
elif[“$1”=”--color=auto”];然后
如果[被重定向或管道化];则
回显“$text”
其他的
echo-e“\e[1;3600万${text}\e[0万”#浅蓝色
fi
fi
我已经做了一些测试,如果我在一个我回显的字符串中指定颜色代码,那么当它被管道传输/重定向时,颜色代码也会被管道传输/重定向(这是我们期望的--color=always
)的行为。此外,如果我不在回显文本中放置颜色代码,这就是我期望的--color=never
的行为(显然)。我不明白的是如何让类似于--color=auto
的东西工作,它可以感知管道和重定向;它的功能如下:
./example.sh --color=auto
=> This is some text # Entire string is colored light blue
./example.sh --color=auto | grep "text"
=> This is some text # Piped text isn't colored so only "text" ends up colored from the grep command (if grep is set to color it)
# parse arguments
color=... # auto | always | never
color_green() {
if ((!use_color)); then return; fi
echo "Output green color code"
}
color_reset() {
if ((!use_color)); then return; fi
echo "Output reset color code"
}
printf_green() {
color_green
printf "$@"
color_reset
printf "\n"
}
use_color=0
# enable coloring when always, or when output is a tty and auto
if [[ "$color" == always || ( "$color" == auto && -t 1 ) ]]; then
use_color=1
fi
printf_green "Hello world"
我不确定如何实现这一点(或者是否可能),谷歌搜索到目前为止也没有任何帮助。我不确定这是否有帮助或者是否相关,但我是linux用户(Ubuntu)使用带有
xterm-256color
的终端,我现在不关心可移植性,但如果终端类型有限制的话,我想知道有什么解决方案。对此的任何指导都将非常感谢。只有当数据输出到终端时,颜色才有意义ode>--color=auto只需检查它是否正在打印到tty/pty。对于Bash,您可以这样编写:
color=... # auto | always | never
...
if [[ $color == auto ]]; then
# 0 for stdin, 1 for stdout, 2 for stderr
if [[ -t 1 ]]; then
# we're writing to a tty so use colors
...
else
# not a tty, no colors
...
fi
fi
其他语言通常会使用类似C的函数来检查打开的文件是否为tty。使用函数。使用变量。正确构造代码。您的代码可能如下所示:
./example.sh --color=auto
=> This is some text # Entire string is colored light blue
./example.sh --color=auto | grep "text"
=> This is some text # Piped text isn't colored so only "text" ends up colored from the grep command (if grep is set to color it)
# parse arguments
color=... # auto | always | never
color_green() {
if ((!use_color)); then return; fi
echo "Output green color code"
}
color_reset() {
if ((!use_color)); then return; fi
echo "Output reset color code"
}
printf_green() {
color_green
printf "$@"
color_reset
printf "\n"
}
use_color=0
# enable coloring when always, or when output is a tty and auto
if [[ "$color" == always || ( "$color" == auto && -t 1 ) ]]; then
use_color=1
fi
printf_green "Hello world"
如果对其工作的终端类型有限制
-t
开关现在和应该在任何地方都可用。它在内部调用。测试是fd 1是一个终端,您基本上是测试用户是否可以看到输出,或者它是否转到另一个程序(即管道)