说明:环境是centos6.5 ip是172.24.10.107 目的是为了学习shell
这次学到的内容:判断安装包是否存在
1
2
3
4
5
|
if
[ `rpm -qa |
grep
httpd |
wc
-l` -
ne
0 ];
then
这个是存在怎么样
else
不存在怎么样
fi
|
首先看下:
1
|
rpm -qa |
grep
httpd
|
看下如果安装了返回的结果:
1
2
3
4
5
|
[root@China shell]
# rpm -qa |grep httpd
httpd-devel-2.2.15-60.el6.centos.6.x86_64
httpd-tools-2.2.15-60.el6.centos.6.x86_64
httpd-2.2.15-60.el6.centos.6.x86_64
httpd-manual-2.2.15-60.el6.centos.6.noarch
|
如果加上wc -l 呢
1
2
|
[root@China shell]
# rpm -qa |grep httpd |wc -l
4
|
返回一个4,就说明安装了4个安装包,也就说明这个软件安装了,-ne的意思是不等于,
在这的意思就是所4不等0,所以软件安装了。
完整的程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#!/bin/bash
#by-duwentao
#
2017
-
11
-
12
html=
"/var/www/html"
ip=
127.24
.
10.107
if
[ `rpm -qa |grep httpd |wc -l` -ne
0
];then
yum remove httpd* -y
yum install httpd* -y
else
yum install httpd* -y
fi
if
[ ! -f $html/index.html ];then
mkdir -p $html
touch $html/index.html
else
rm -rf $html
mkdir -p $html
touch $html/index.html
chmod
705
$html/index.html
fi
cat >>$html/index.html<< +END+
this
is
$ip server
+END+
echo
"ServerName $ip:80"
>> /etc/httpd/conf/httpd.conf
service httpd restart
|
执行效果:
欢迎关注我微信公众号,与我一起学习
本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/1980985