我正在开发一些shell脚本。请帮帮我
我想用这样的FUNCTION突出显示特定的字符串。
#!/bin/bash
function echoWithColor(){
local InputString=${1}
tput bold;tput setaf 2 # make words color to bright green
echo ${InputString}
tput sgr0 # make words color to default
}
echoWithColor "Hello everyone!"
这有效,但我想在行尾添加一些空白(空格文本),如
echo -n "Input your age : "; read AGE
[root @ mycomputer scripts]#。/ script.sh
输入您的年龄:9999
但
#!/bin/bash
function echoWithColor(){
local InputString=${1}
tput bold;tput setaf 2 # make words color to bright green
echo ${InputString}
tput sgr0 # make words color to default
}
echoWithColor "-n Input your age : "; read AGE
[root @ mycomputer scripts]#。/ script.sh
输入您的年龄:9999
都能跟得上
#!/bin/bash
function echoWithColor(){
local InputString=${1}
tput bold;tput setaf 2 # make words color to bright green
echo ${InputString}
tput sgr0 # make words color to default
}
echoWithColor "-n \"Input your age : \""; read AGE
[root @ mycomputer scripts]#。/ script.sh
"输入您的年龄:" 9999
逃逸?不。
如何在行尾添加空白文字?
答案 0 :(得分:0)
只需添加空间' '在echo ${InputString}
命令中的函数如下所示
#!/bin/bash
function echoWithColor(){
local InputString=${1}
tput bold;tput setaf 2 # make words color to bright green
echo ${InputString}' '
tput sgr0 # make words color to default
}
echoWithColor "-n Input your age : "; read AGE
答案 1 :(得分:0)
这是因为您没有引用变量:
var=" my value "
echo $var without quotes # writes: my value without quotes
echo "$var with quotes" # writes: my value with quotes
由于您希望将多个字符串传递给echo(-n
和Input your age
),因此您应该重写该函数以获取多个参数("$@"
)而不是一个($1
) {1}}),然后确保在使用它们时引用:
#!/bin/bash
echoWithColor(){
local InputStrings=( "$@" )
tput bold;tput setaf 2 # make words color to bright green
echo "${InputStrings[@]}"
tput sgr0 # make words color to default
}
echoWithColor -n "Input your age: "
现在echoWithColor
与echo
完全相同,并保留您传递给它的所有空格。