
我正在編寫腳本來初始化和配置具有許多組件的大型系統。
每個元件都有自己的日誌檔。
每當安裝/配置發生錯誤時,我想將元件檔案名稱的顏色變更為紅色。
我該怎麼做?
答案1
谷歌會給你找到答案。用紅色印製 Hello world:
echo -e "\033[0;31mHello world\033[0m"
解釋
<esc><attr>;<foreground>m
<esc> = \033[ ANSI escape sequence, some environments will allow \e[ instead
<attr> = 0 Normal text - bold possible with 1;
<foreground> = 31 30 + 1 = Color red - obviously!
m = End of sequence
\033[0m Reset colors (otherwise following lines will be red too)
看著http://en.wikipedia.org/wiki/ANSI_escape_code取得顏色和其他功能的完整清單(粗體等)。
命令 tput(如果可用)將使生活變得更輕鬆:
echo -e "$(tput setaf 1)Hello world$(tput sgr0)"
甚至可以將序列保存在變數中以方便使用。
ERR_OPEN=$(tput setaf 1)
ERR_CLOSE=$(tput sgr0)
echo -e "${ERR_OPEN}Hello world${ERR_CLOSE}"
答案2
如果您有興趣將顏色設為變量,以下內容可能有助於為 bash 腳本設定顏色面板
COL_BLACK="\x1b[30;01m"
COL_LIGHTBLACK="\x1b[30;11m"
COL_BLUE="\x1b[34;01m"
COL_LIGHTBLUE="\x1b[34;11m"
COL_CYAN="\x1b[36;01m"
COL_LIGHTCYAN="\x1b[36;11m"
COL_GRAY="\x1b[37;11m"
COL_LIGHTGRAY="\x1b[37;01m"
COL_GREEN="\x1b[32;01m"
COL_LIGHTGREEN="\x1b[32;11m"
COL_PURPLE="\x1b[35;01m"
COL_LIGHTPURPLE="\x1b[35;11m"
COL_RED="\x1b[31;01m"
COL_LIGHTRED="\x1b[31;11m"
COL_YELLOW="\x1b[33;01m"
COL_LIGHTYELLOW="\x1b[33;11m"
COL_RESET="\x1b[39;49;00m"
答案3
http://webhome.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html。
這可能就是你想要的。
這樣就可以得到$?
一個元件檔案的進程,然後選擇使用
echo -e "\e[1;31m"<the-component-file-name>\e[0m"
使其文字變為紅色。