Skip to content

Latest commit

 

History

History
42 lines (38 loc) · 686 Bytes

48.md

File metadata and controls

42 lines (38 loc) · 686 Bytes

题目要求

写一个shell,先判断是否安装httpd和mysql,没有安装进行安装,安装了检查是否启动服务,若没有启动则需要启动服务。

参考答案

#!/bin/bash
if_install()
{
    rpm -q $1 >/dev/null 2>/dev/null 
    if [ $? -eq 0 ]
    then
	echo "$1已经安装"
	return 0
    else
	echo "$1没有安装"
	return 1
    fi
}

if_install httpd
if [ $? -eq 0 ]
then
    if ! pgrep httpd >/dev/null 
    then
	service httpd start
    fi
else
    yum install -y httpd
fi

if_install mysql-server
if [ $? -eq 0 ]
then
    if ! pgrep mysqld >/dev/null
    then
       service mysqld start
    fi
else
    yum install -y mysql-server
fi