如何避免根据终端类型打印转义序列?

发布于 2024-10-21 00:21:21 字数 529 浏览 7 评论 0原文

我编写了一个测试脚本,通常从 cygwin bash 调用它。该测试脚本使用转义序列对输出进行着色,即

OKSTRING="\E[32;42mOK \033[39;49m"
echo -e "  $OKSTRING"

在成功的情况下。实际上,字符串 OK 是不可见的,因为前景和背景具有相同的颜色。所以,我只是输出一个绿色、黄色或红色的框来指示测试结果。

现在,我想运行与 Hudson 构建步骤相同的脚本。转义码使输出很难可读。

有什么方法可以影响转义码的打印而不必向测试脚本本身传递更多信息?

我知道我可以使用命令行参数并重新定义OKSTRING 当我传递某个参数时。但我正在寻找一些更简单的方法(即在脚本外部设置一些环境变量,并且 echo 知道 它不应该打印转义序列)。

I've written a test script that I usually call from a cygwin bash. This test script colors it's output with escape sequences, i.e.

OKSTRING="\E[32;42mOK \033[39;49m"
echo -e "  $OKSTRING"

in case of a success. Actually, the string OK isn't visible since foreground and background have the same color. So, I just output a green, yellow or red box to indicate the test result.

Now, I want to run the same script as a Hudson build step. The escape codes make the output quite hard readable.

Is there any way that I could influence the printing of the escape codes without having to pass further information to the test script itself?

I know that I could do it using command line parameters and redefine OKSTRING when I pass a certain parameter. But I'm looking for some easier way (i.e. set some environment variable outside the script and echo knows that it shouldn't print the escape sequences).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄风 2024-10-28 00:21:21
if [ -t 1 ]
then
  echo -e "\E[32;42mstdout is a tty"
else
  echo "stdout is not a tty"
fi
if [ -t 1 ]
then
  echo -e "\E[32;42mstdout is a tty"
else
  echo "stdout is not a tty"
fi
阳光下的泡沫是彩色的 2024-10-28 00:21:21

据我了解你的问题,如果你从命令行手动运行脚本,你希望它打印颜色代码,而当它通过 Hudson 构建服务器运行时,你希望它只打印纯文本。

您可以使用 isatty,库调用,尽管您可能有情况,但这并不完全是您想要的(即它可能会错过某些情况)。 Bash 提供了对 -t 测试的访问

-t fd
    True if file descriptor fd is open and refers to a terminal. 

,根据 LDP,以下是测试交互式/非交互式的首选方法

if [[ -t 0 || -p /dev/stdin ]]
then
  echo interactive
else
  echo non-interactive
fi

As I understand your question, in the case you run the script manually from the command line you want to have it print colour codes, while when it is run through the Hudson build server you want it to just print plain text.

You can test if the terminal is considered a tty with the isatty, library call although you might have cases this will not be exactly what you want (i.e. it might miss some cases). Bash provides access to a -t test

-t fd
    True if file descriptor fd is open and refers to a terminal. 

Accorting to LDP, the following is the preffered way to test for interactive/non-interactive

if [[ -t 0 || -p /dev/stdin ]]
then
  echo interactive
else
  echo non-interactive
fi
或十年 2024-10-28 00:21:21

如果您使用tput而不是硬编码转义序列,则可以设置TERM变量来控制是否输出颜色代码。它的优点是可以将输出移植到 xterm 以外的终端。

$ printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S; echo -n hi; tput sgr0; echo
[green box]
$ (TERM=dumb; printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S; echo -n hi; tput sgr0; echo)
hi

第二个在子 shell 中运行,因此当子 shell 退出时,TERM 返回到其先前的值。

为了避免重复调用 tput,您可以在脚本开头调用几次并设置变量来保存序列:

allgreen=$(printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S)
none=$(tput sgr0)
echo "${allgreen}hi${none}"

然后您可以在子 shell 中调用脚本或在变量前面添加变量作业(或自己保存并恢复TERM)。

$ TERM=dumb ./your_script

这使得分配对于该调用是本地的。

If you use tput instead of hard-coding the escape sequences, you can set the TERM variable to control whether color codes are output. It has the bonus of making the output portable to terminals other than xterm.

$ printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S; echo -n hi; tput sgr0; echo
[green box]
$ (TERM=dumb; printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S; echo -n hi; tput sgr0; echo)
hi

The second one is run in a subshell so TERM returns to its previous value when the subshell exits.

In order to avoid repeatedly calling tput, you can call it just a few times at the beginning of your script and set variables to hold the sequences:

allgreen=$(printf '%s\n%s\n' "setaf 2" "setab 2" | tput -S)
none=$(tput sgr0)
echo "${allgreen}hi${none}"

Then you can call your script in a subshell or prepend the variable assignment (or save and restore TERM yourself).

$ TERM=dumb ./your_script

That makes the assignment local to that invocation.

故人的歌 2024-10-28 00:21:21

为了变得更加健壮,您需要检测交互性和终端功能。 -t 测试检测交互性。 tput 检测终端功能:

[[ -t 1 ]] || export TERM=dumb
CYAN=$(tput setf 3)
GRAY=$(tput setf 7)

echo ${CYAN}COLORED TEXT${GRAY} 

这样只有您检测交互性,而实际上您应该检测交互性(通过 -t 和终端功能(通过$TERM/tput)。

To be more robust, you need detect both interactivity and terminal capabilities. The -t test detects the interactivity. The tput detects the terminal capabilities:

[[ -t 1 ]] || export TERM=dumb
CYAN=$(tput setf 3)
GRAY=$(tput setf 7)

echo ${CYAN}COLORED TEXT${GRAY} 

This way only you detect interactivity while in reality you should detect both interactivity (through -t and terminal capabilities (through $TERM/tput).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文