来自变量的Bash脚本printf颜色?

时间:2021-06-15 23:20:50

I was hoping to see if someone can point out what I am doing wrong because I am stumped. I am trying to learn bash scripting to deploy a dev test server. One of the things I am trying to achieve is outputting status outputs when a part of the script completes or an action completes. As an example:

我希望有人能指出我做错了什么,因为我被难住了。我正在尝试学习使用bash脚本部署开发测试服务器。我试图实现的一件事是在脚本的一部分完成或操作完成时输出状态输出。作为一个例子:

    printf "[ %s ] %s distribution detected. Validated as supported by this script.\n" "$PASSED" "$LINUX_DISTRO"

The problem I am having is formatting the value of the first string, %s, which is $PASSED. In the beginning of the script I have defined $PASSED as

我遇到的问题是格式化第一个字符串%s的值,它是$ passing。在脚本的开头,我定义了$ pass as

PASSED="\e[32mPASSED\e[0m"

However, when I go to execute the script, the output looks like the following:

但是,当我执行脚本时,输出如下所示:

[ \e[32mPASSED\e[0m ] CENTOS distribution detected. Validated as supported by this script.

Instead of the correct output which looks like:

而不是正确的输出:

[ PASSED ] CENTOS distribution detected. Validated as supported by this script.

Where "PASSED" is written in green coloring.

“pass”是用绿色写的。

However, I did notice that if I do the following:

然而,我确实注意到,如果我这样做:

printf "[ $PASSED ] %s distribution detected. Validated as supported by this script.\n" "$LINUX_DISTRO"

The above behaves correctly, but I believe isn't the correct way of doing it. Any ideas?

上述行为是正确的,但我认为这不是正确的做法。什么好主意吗?

2 个解决方案

#1


1  

You want bash to interpret the backslash characters. Use %b instead of %s in your printf. So instead you should have the following.

您希望bash解释反斜杠字符。在您的printf中使用%b而不是%s。所以你应该有以下内容。

printf "[ %b ] %s distribution detected. Validated as supported by this script.\n" $PASSED $LINUX_DISTRO

Also, I modified your variable.

另外,我修改了变量。

PASSED="\e[00;32mPASSED\e[0m"

#2


1  

Another (non-standard) solution is to put the escape character directly in the original string, rather than \e for printf to interpret.

另一种(非标准的)解决方案是将转义字符直接放入原始字符串中,而不是将printf解析为\e。

PASSED=$'\e[32mPASSED\e[0m'
printf '[ %s ] some test\n' "$PASSED"

However, this assumes that your script is run by /bin/bash, not /bin/sh (which might be bash, but run in POSIX-compliant mode).

但是,这假定您的脚本是由/bin/bash运行的,而不是/bin/sh(可能是bash,但运行在与posix兼容的模式中)。

Using %b has the same problem: it is specific to bash, and you may not want to assume that bash is the shell executing your script.

使用%b也有同样的问题:它是特定于bash的,您可能不希望假设bash是执行脚本的shell。

A fully POSIX-compliant solution is to use printf itself to create the value of PASSED.

一个完全符合posix的解决方案是使用printf本身来创建传递的值。

# \e not supported by POSIX; use \033 (octal) instead.
PASSED=$(printf '\033[32mPASSED\033[0m')
printf '[ %s ] some test\n' "$PASSED"

#1


1  

You want bash to interpret the backslash characters. Use %b instead of %s in your printf. So instead you should have the following.

您希望bash解释反斜杠字符。在您的printf中使用%b而不是%s。所以你应该有以下内容。

printf "[ %b ] %s distribution detected. Validated as supported by this script.\n" $PASSED $LINUX_DISTRO

Also, I modified your variable.

另外,我修改了变量。

PASSED="\e[00;32mPASSED\e[0m"

#2


1  

Another (non-standard) solution is to put the escape character directly in the original string, rather than \e for printf to interpret.

另一种(非标准的)解决方案是将转义字符直接放入原始字符串中,而不是将printf解析为\e。

PASSED=$'\e[32mPASSED\e[0m'
printf '[ %s ] some test\n' "$PASSED"

However, this assumes that your script is run by /bin/bash, not /bin/sh (which might be bash, but run in POSIX-compliant mode).

但是,这假定您的脚本是由/bin/bash运行的,而不是/bin/sh(可能是bash,但运行在与posix兼容的模式中)。

Using %b has the same problem: it is specific to bash, and you may not want to assume that bash is the shell executing your script.

使用%b也有同样的问题:它是特定于bash的,您可能不希望假设bash是执行脚本的shell。

A fully POSIX-compliant solution is to use printf itself to create the value of PASSED.

一个完全符合posix的解决方案是使用printf本身来创建传递的值。

# \e not supported by POSIX; use \033 (octal) instead.
PASSED=$(printf '\033[32mPASSED\033[0m')
printf '[ %s ] some test\n' "$PASSED"