shell函数-页面跳转练习->

实现思维导图->

 

实现思路->

分析:
1:先把三个页面的流程作为函数先写下来,定义在脚本的开头,方便下面的调用。
2:先从一个流 程开始做,其他的流程类似,比如nginx
3:整体实现思路是 ,函数调用 , while do 循环, case in

 

实现脚本->(仅作为参考练习)

  1 #!/usr/bin/bash
  2 
  3 Page_first () {
  4 cat << EOF
  5 ================
  6 1. 安装Nginx
  7 6. 退出
  8 ================
  9 EOF
 10 }
 11 
 12 Page_Nginx () {
 13 cat << EOF
 14 ================
 15 1. 安装Nginx 1.12
 16 2. 源码编译Nginx 1.12
 17 3. 返回上一个页面
 18 4. 退出
 19 ================
 20 EOF
 21 }
 22 
 23 Page_Nginx_Install () {
 24 cat << EOF
 25 ================
 26 1. 配置yum仓库
 27 2. 安装Nginx
 28 3. 启动Nginx
 29 4. 返回上一个月面
 30 5. 退出
 31 ================
 32 EOF
 33 }
 34 
 35 
 36 Page_Nginx_Install_make () {
 37 cat << EOF
 38 ================
 39 1. 准备基础环境,安装依赖包
 40 2. 下载Nginx编译包
 41 3. ./configure
 42 4. make
 43 5. make install
 44 6. 启动
 45 7. 返回上级菜单
 46 8. 退出
 47 ================
 48 EOF
 49 }
 50 
 51 
 52 while true
 53 do
 54 clear
 55 Page_first
 56 read -p "请选择需要安装的编号:" First_page
 57 case $First_page in
 58         1)
 59             clear
 60             while true
 61             do
 62             Page_Nginx
 63             read -p "请选择安装方式:" Nginx_page
 64             case $Nginx_page in
 65                     1)
 66                         clear
 67                         while true
 68                         do
 69                         Page_Nginx_Install
 70                         read -p "请输入进行的步骤:" Nginx_Install
 71                         case $Nginx_Install in
 72                             1)
 73                                 clear
 74                                 if [ -f /etc/yum.repos.d/nginx.repo ];then
 75                                     echo "yum仓库已有,无需进行配置"
 76                                 else
 77                                     cat >/etc/yum.repos.d/nginx.repo <<EOF
 78 [nginx]
 79 name=nginx repo
 80 baseurl=http://nginx.org/pacs/centos/7/x86_64/
 81 gpgcheck=0
 82 enabled=1
 83 EOF
 84                                     echo "Nginx的yum仓库配置完成"
 85                                 fi
 86                                 ;;
 87                             2)
 88                                 clear
 89                                 nginx -v &>/dev/null
 90                                 if [ $? -eq 0 ];then
 91                                     echo "Nginx已安装,无需进行安装"
 92                                 else
 93                                     echo "正在安装Nginx"
 94                                     yum -y install nginx &>/dev/null
 95                                     if [ $? -ne 0 ];then
 96                                         echo "Nginx安装失败,请查找原因"
 97                                     else
 98                                         echo "Nginx安装成功"
 99                                     fi
100                                 fi
101                                 ;;
102                             3)
103                                 clear
104                                 nginx_status=$(systemctl status nginx 2>/dev/null |grep running |wc -l)
105                                 if [ $nginx_status -eq 1 ];then
106                                     echo "Nginx已启动,无需启动"
107                                 else
108                                     systemctl start nginx  &>/dev/null
109                                     if [ $? -ne 0 ];then
110                                         echo "Nginx启动失败,请查找原因"
111                                     else
112                                         echo "Nginx启动成功"
113                                     fi
114                                 fi
115                                 ;;
116                             4)
117                                 clear
118                                 break
119                                 ;;
120                             5)
121                                 exit
122                                 ;;
123                             *)
124                                 clear
125                                 echo "输入错误,请重新输入"
126                         esac
127                         done                            
128                         ;;
129                     2)
130                         clear
131                         while true
132                         do
133                         Page_Nginx_Install_make
134                         read -p "请输入进行的步骤:" Nginx_make
135                         case $Nginx_make in
136                             1)
137                                 clear
138                                 echo "正在准备基础环境"
139                                 id www &>/dev/null 
140                                 if [ $? -eq 0 ];then
141                                     echo "基础环境准备完成"
142                                 else
143                                     groupadd -g 666 www && useradd -u 666 -g www www -s /sbin/nologin -M
144                                     if [ $? -ne 0 ];then
145                                         echo "基础环境准备失败"
146                                     else
147                                         echo "基础环境准备完成"
148                                     fi                                    
149                                 fi
150                                 echo "正在安装依赖包"
151                                 yum install wget gcc make  pcre-devel  openssl-devel zlib-devel -y &>/dev/null
152                                 if [ $? -ne 0 ];then
153                                     echo "依赖包安装失败,请查找原因"
154                                 else
155                                     echo "依赖包安装成功"
156                                 fi
157                                 ;;
158                             2)
159                                 clear
160                                 echo "正在下载Nginx"
161                                 [ -d /server/tools ] || mkdir /server/tools -p
162                                 cd /server/tools
163                                 wget http://nginx.org/download/nginx-1.14.0.tar.gz &>/dev/null && \
164                                 tar xf nginx-1.14.0.tar.gz &>/dev/null && \
165                                 cd nginx-1.14.0
166                                 if [ $? -ne 0 ];then
167                                     echo "Nginx下载或解压失败,请查看原因"
168                                 else
169                                     echo "Nginx下载成功"
170                                 fi
171                                 ;;
172                             3)
173                                 clear
174                                 echo "正在执行./configure"
175                                 ./configure --prefix=/application/nginx-1.14 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module &>/dev/null
176                                 if [ $? -ne 0 ];then
177                                     echo "Nginx执行./configure失败,请查看原因"
178                                 else
179                                     echo "Nginx执行./configure成功"
180                                 fi
181                                 ;;
182                             4)        
183                                 clear
184                                 echo "正在执行make"
185                                 make &>/dev/null
186                                 if [ $? -ne 0 ];then
187                                     echo "Nginx执行make失败,请查看原因"
188                                 else
189                                     echo "Nginx执行make成功"
190                                 fi
191                                 ;;
192                             5)        
193                                 clear
194                                 echo "正在执行make install"
195                                 make install &>/dev/null
196                                 if [ $? -ne 0 ];then
197                                     echo "Nginx执行make install失败,请查看原因"
198                                 else
199                                     echo "Nginx执行make install成功"
200                                 fi
201                                 ;;
202                             6)
203                                 
204                                 ;;
205                             7)
206                                 clear
207                                 break
208                                 ;;
209                             8)
210                                 exit
211                                 ;;
212                             *)
213                                 clear
214                                 echo "输入错误,请重新输入"
215                         esac
216                         done                            
217                         ;;                                    
218                     3)
219                         clear
220                         break
221                         ;;
222                     4)
223                         exit
224                         ;;
225                     *)
226                         clear
227                         echo "输入错误,请重新输入"
228                 esac
229                 done
230             ;;
231         6)
232             exit
233             ;;
234         *)
235             clear
236             echo "输入错误,请重新输入"
237     esac
238 done

 

posted @ 2018-09-25 11:32  少校的小木屋  阅读(968)  评论(0编辑  收藏  举报