master="192.168.56.200 master"
slave1="192.168.56.201 slave1"
slave2="192.168.56.202 slave2"
slave3="192.168.56.203 slave3"
4'string' 单引号和"string" 双引号双引号
如果想在定义的变量中加入空格,就必须使用单引号或双引号,
单、双引号的区别在于双引号转义特殊字符而单引号不转义特殊字符
eg: $ heyyou=home
$ echo '$heyyou'
$ $heyyou ($没有转义)
eg: $ heyyou=home
$ echo "$heyyou"
$ home (很明显,$转义了输出了heyyou变量的值)
5、对字符串判空
-z string 如果 string 长度为零,则为真 [ -z $myvar ]
-n string 如果 string 长度非零,则为真 [ -n $myvar ]
6、将字符串转换为数组
#host config
for node in "$MASTER" "$MASTER_HA" "$SLAVE1" "$SLAVE2" "$SLAVE3"; do
array=( $node )
# if host or ip is not exist in /etc/hosts, then add.
if [ -z "`grep "${array[0]}" /etc/hosts`" -o -z "`grep "${array[1]}" /etc/hosts`" ]; then
echo ${array[@]} >> /etc/hosts
fi
done