如何为该脚本添加自定义颜色?

如何为该脚本添加自定义颜色?

这是脚本。有人知道我如何更改文本的颜色,以便当我在终端中打开它时它会改变颜色吗?

#!/usr/bin/expect -f
spawn -noecho bash
expect "$ " 
send "put what ever you want here"
interact
exit

我试过了,但是终端上显示的是

'PROMPT_BLUE=`tput setf 1`
PS1='[$PROMPT_BLUE]'
john@john:~$ PROMPT_BLUE=`tput setf 1`
john@john:~$ PS1='[$PROMPT_BLUE]'
[]perl put what ever you want here' 

在此处输入图片描述

这就是出现的结果,我不知道为什么哈哈哈

答案1

也许这就是你想要的:

#!/bin/bash

PROGNAME=${0##*/}

# colors:
BLUE='\033[94m'
GREEN="\e[0;32m"
RED='\033[91m'
BLACK='\033[0m'

# program:
echo -e "${RED}${PROGNAME}: ${GREEN}Done.${BLACK}"

编辑

最小工作示例expect

#!/usr/bin/expect -f 

# colors:
# BLUE='\033[94m'
# GREEN="\e[0;32m"
# RED='\033[91m'
# BLACK='\033[0m'

# we need byte code. Let's get one for the blue:
# python -c "print ''.join([r'\x%x' % ord(c) for c in \"\033[94m\"])"
# gives: \x1b\x5b\x39\x34\x6d
set blue "\x1b\x5b\x39\x34\x6d"

# program:
send "${blue}hello world\n"

编辑2

哦,我解决了:

#!/usr/bin/expect -f 

spawn -noecho bash
send "PROMPT_BLUE=`tput setf 1`\nPS1='\[\$PROMPT_BLUE\]'\n"
expect "$ " 
send "put what ever you want here"
interact
exit

使所有文本变为蓝色。

相关内容