如何在bash脚本中着色命令输出?

时间:2019-07-18 10:22:16

标签: linux bash colors command output

我正在尝试几天以更改bash脚本中命令的颜色输出。我尝试了一些工作流程陷阱,但没有成功。唯一起作用的部分是这段代码:

#!/bin/bash

GRN='\e[32m'
CYN='\e[36m'
END='\e[0m'

echo -e "${GRN}Formating Root partition ..${END}"

echo -e "${CYN}"
(set -x ; mkfs.ext4 -L Root -m 5 /dev/sda2) | GREP_COLOR='49;38;5;007' grep --color=always '.*' 
echo -e "${END}"

echo -e "${GRN}Formating Home partition ..${END}"

...

是否有更好的方法来做到这一点。谢谢。

我想要的是这个

Formating Root partition ..                                    <= Green
mkfs.ext4 -L Root -m 5 /dev/sda2                               <= Cyan
mke2fs 1.45.2 (27-May-2019)                                    <= Grey
Creating filesystem with 9175040 4k blocks and 2293760 inodes  <= Grey
Filesystem UUID: ...                                           <= Grey
...                                                            <= Grey
Formating Home partition ..                                    <= Green

1 个答案:

答案 0 :(得分:0)

您可以使用在Mac和ubuntu上经过测试的以下脚本。

每个函数接受两个参数,

  1. 用于示例字符串输出,例如 private void Button2_Click_1(object sender, EventArgs e) { c1 = JsonConvert.DeserializeObject<Class1>(File.ReadAllText("path.txt")); Console.WriteLine("Current Status of Flag1: "+c1.flag); Console.WriteLine("Current Status of Flag2: " + c1.flag2); }
  2. 打印命令输出,例如green "will print green"

bash脚本

red "will print Free space in red:" "free -m"  free -m

enter image description here

相关问题