击:彩色输出,带可变 [英] Bash: Colored Output with a Variable

查看:80
本文介绍了击:彩色输出,带可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

function pause #for prompted pause until ENTER
{


prompt="$3"
    echo -e -n "\E[36m$3" #color output text cyan
    echo -e -n '\E[0m' #ends colored output
    read -p "$*"  #read keys from user until ENTER.
    clear

}

pause "Press enter to continue..."

不过,我的函数拒绝青色适用于我传递到函数的字符串。

However, my function refuses to apply the cyan color to the string I pass into the function.

有一个类似的问题被问 href=\"http://stackoverflow.com/questions/2829276/bash-color-variable-output\">,但似乎我做正确的一切...

A similar question was asked here, but it seems that I'm doing everything correctly...

推荐答案

我已经略有改变您的code:

I've slightly changed your code:

#!/bin/bash

function pause() {
    prompt="$1"
    echo -e -n "\033[1;36m$prompt"
    echo -e -n '\033[0m'
    read
    clear
}

pause "Press enter to continue..."

我已经改变了:


  1. 您被初始化提示$ 3,当正确的论点是$ 1

  2. 的ANSI序列是不正确的。参见:<一href=\"http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html\">http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

  3. 要读取的调用是不正确的,你路过几个参数做使用$ *。在这种特殊情况下你丢弃输入,所以它甚至没有必要保存读取的结果。我建议你​​阅读手册页: http://linux.die.net/man/1/bash 以看看如何准确使用读。如果您在多个传递参数,这些参数将被映射到将包含在该行输入的不同字段变量名。

  1. You were initializing prompt to $3, when the correct argument was $1
  2. The ANSI sequence was incorrect. See: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
  3. The call to read was incorrect, you were passing several arguments do to the use of $*. In this particular case you are discarding the input, so it's not even necessary to save the result of read. I suggest you to read the manpage: http://linux.die.net/man/1/bash to see how to exactly use read. If you pass in several arguments, those arguments will be mapped to variable names that will contain the different fields inputted in the line.

这篇关于击:彩色输出,带可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆