您的位置:首页 > 运维架构 > Shell

shell小节

2015-10-05 18:48 330 查看
1、shell中,获取一个命令的执行结果
a=`ps -ef | grep tomcat`
注意:“ ` ”这个符号不是单引号,而是esc下面的那个小撇

2、读取配置文件
[root@master local]# vi host.conf

master="192.168.56.200 master"

slave1="192.168.56.201 slave1"

slave2="192.168.56.202 slave2"

slave3="192.168.56.203 slave3"
读取:
source ./host.conf
读取每个变量:
echo $master >> /etc/hosts

3、vi显示行号
:set nu

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: