好的,所以我尝试使用变量值然后使用该变量的值来获取该变量的值。
所以,我script
把它$1
当作一条信息,然后把它$2
当作打印出来的颜色。
#!/bin/bash
# TAKES $1 as MSG and $2 as COLOR.
export black='echo -e "\E[30;47m"'
export red='echo -e "\E[31;47m"'
export green='echo -e "\E[32;47m"'
export yellow='echo -e "\E[33;47m"'
export blue='echo -e "\E[34;47m"'
export magenta='echo -e "\E[35;47m"'
export cyan='echo -e "\E[36;47m"'
export white='echo -e "\E[37;47m"'
# VARIABLES
export color="$2" # Set Color
export message="$1" # Set MSG
# Use $color for what variable to substitute with, so we get the echos.
# Previously tried \$$color to get $whatever_$2_was but it wouldn't get the value.
\$$color
echo "$message" # Echo Message.
tput sgr0 # Reset to normal.
它被称为:
USAGE: cecho "MESSAGE" color
所以,基本上脚本点是$message
用任何$2
相等的颜色。
Anyidea如何:
1.$2
获取带有名字的变量的值,
或者
2.更好的写法script
?