ads2 cls create | awk -F [\"] '/^>>> cluster established .../ { strt=1 } strt!=1 { next } $1 ~ "name" { cnt++;nam[cnt]=$2 } $1 ~ "state" {stat[cnt]=$2;print -e "Node "nam[cnt]" has state "$2 } END { if (stat[1]=="3" && stat[2]=="3") { print "Cluster established. Starting session manager" } else { print "Failed" } }
上述命令给出了输出ads2 cls create
,awk
检查特定的输出变量,并根据该输出返回"Cluster established. Starting session manager"
或Failed
但是,我尝试为green
第一个输出着色,为第二个输出着色。我在互联网上搜索,没有找到任何与使用print
命令打印的彩色文本相关的内容,而是使用echo -e
命令。
那么,我怎样才能使用当前的实现来为这两个输出着色!
命令的输出ads2 cls create
如下所示
kernel with pid 3336 (port 9011) killed
kernel with pid 16406 (port 9011) killed
probing service daemon @ http://xx.xx.x.xx:9010
starting kernel FDT-NB-0144 @ http://xx.xx.0.xx:9011 name=xx-NB-0144 max_consec_timeouts=10 clustermode=Standard hostname=FDT-NB-0144 framerate=20000 schedmode=Standard rtaddr=xx.xx.x.xx tickrole=Local tickmaster=local max_total_timeouts=1000
kernel FDT-NB-0144 running
probing service daemon @ http://xx.xx.xx.xx:9010
starting kernel fdt-c-agx-0008 @ http://xx.xx.xx.18:9011 name=fdt-c-agx-0008 max_consec_timeouts=10 clustermode=Standard hostname=nvidia@fdt-c-agx-0008 framerate=20000 schedmode=Standard rtaddr=xx.xx.xx.xx tickrole=Local tickmaster=local max_total_timeouts=1000
kernel fdt-c-agx-0008 running
>>> start cluster establish ...
>>> cluster established ...
nodes {
node {
name = "xxx-c-agx-0008";
address = "http://xx.xx.xx.xx:9011/";
state = "3";
}
node {
name = "xxx-NB-0144";
address = "http://xx.xx.0.xx:9011/";
state = "3";
}
}
答案1
您可以在 awk printf 命令中使用 ANSI 颜色序列,如下所示:
awk 'BEGIN{printf "\x1b[32m%s\x1b[0m\n", "Succeeded"}'
awk 'BEGIN{printf "\x1b[31m%s\x1b[0m\n", "Failed"}'
前任:
要将其应用于您的情况,请将END
块修改为:
END {
if (stat[1]=="3" && stat[2]=="3") {
printf "\x1b[32mCluster established. Starting session manager\x1b[0m\n" }
else {
printf "\x1b[31mFailed\x1b[0m\n"
}
}