Shell的流程控制

流程控制基本概述

1. 单分支结构
[root@shell /scripts]# cat if-1.sh
#!/bin/bash
if [ $1 -gt 10 ];then
  echo   "条件成立"
fi
[root@shell /scripts]# sh if-1.sh  11
条件成立
[root@shell /scripts]# sh if-1.sh  9
[root@shell /scripts]# 


2. 双分支结构
[root@shell /scripts]# cat if-2.sh
#!/bin/bash
if [ $1 -gt 10 ];then
  echo   "条件成立"
else
    echo "条件不成立"
fi
[root@shell /scripts]# sh if-2.sh 11
条件成立
[root@shell /scripts]# sh if-2.sh 9
条件不成立

3. 多分支结构
[root@shell /scripts]# cat if-3.sh
#!/bin/bash
read -p "请输入一个你要查验的用户:" User
if grep "^$User" /etc/passwd &>/dev/null;then
    echo "用户$User 存在该系统......"
    if ls /home/$User &>/dev/null;then
        echo "用户$User 家目录也存在....."
    else
        echo "用户$User 家目录不存在....."
    fi 
elif ls /home/$User &>/dev/null;then
    echo "当前用户$User 不存在该系统....."
    echo "但是用户$User 存在家目录......."
else
    echo "用户$User 不存在该系统........"
    echo "用户$User 也不存在家目录......"
fi
[root@shell /scripts]# sh if-3.sh
请输入一个你要查验的用户:www
用户www 存在该系统......
用户www 家目录也存在.....
[root@shell /scripts]# sh if-3.sh
请输入一个你要查验的用户:test
当前用户test 不存在该系统.....
但是用户test 存在家目录.......
[root@shell /scripts]# id test
id: test: no such user
[root@shell /scripts]# ll /home/
total 0
drwx------ 2 user user 62 2020-02-21 16:07 test
drwx------ 2 www  www  62 2020-02-21 16:06 www
[root@shell /scripts]# sh if-3.sh
请输入一个你要查验的用户:user
用户user 存在该系统......
用户user 家目录不存在.....
[root@shell /scripts]# id user
uid=1001(user) gid=1001(user) groups=1001(user)

[root@shell /scripts]# cat var-2.sh
#!/bin/bash
#1.定义变量
String='Bigdata process is Hadoop, Hadoop is open source project'
#2.打印变量
echo $String
#3.输出菜单
cat<<EOF
1)打印string长度
2)删除字符串中所有的Hadoop
3)替换第一个Hadoop为Linux
4)替换全部Hadoop为Linux
EOF
#4.提示用户输入对应的数字,执行对应的功能
read -p "请输入上方菜单对应的数字,执行其对应的功能[1|2|3|4]:" Num
#5.根据用户输入的数字进行执行对应的功能
if [ $Num -eq 1 ];then
    echo "正在打印变量String的长度....."
    echo ${#String}
elif [ $Num -eq 2 ];then
    echo "正在删除字符串中的所有Hadoop"
    echo ${String//Hadoop/}
elif [ $Num -eq 3 ];then
    echo "正在替换变量中第一个Hadoop为Linux"
    echo ${String/Hadoop/Linux}
elif [ $Num -eq 4  ];then
    echo "正在替换变量中的所有Hadoop为Linux"
    echo ${String//Hadoop/Linux}
else
    echo "你输入的数字不正确!请按照要求输入对应的数字!"
fi

2. 流程控制语句之文件比较

-e      如果文件或者目录存在则为真                       [  -e  file/dir ]  

-f      如果文件存在则为真

-s        如果文件存在且至少有一个字符则为真   判断一个文件是否是空文件

-d         如果目录存在则为真  

-r        读的权限

-w        写的权限

-x        执行的权限

#示例


[root@shell /scripts]# [ -e /etc/hosts ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [ -e /etc ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [ ! -e /etc ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ ! -e /etc/hosts ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ -f /etc/hosts ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [ -f /etc/hostss ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# touch  test.log
[root@shell /scripts]# ll test.log
-rw-r--r-- 1 root root 0 2020-02-24 09:01 test.log
[root@shell /scripts]# [ -f test.log ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [  ! -f test.log ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [  ! -s test.log ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [  ! -s  /etc/hosts ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ -s  /etc/hosts ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [ -s  /etc/hostss ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ -d  /etc/hosts ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ -d  /etc ] && echo "为真"  || echo  "为假"
为真
[root@shell /scripts]# [ -d  /etcc ] && echo "为真"  || echo  "为假"
为假
[root@shell /scripts]# [ ! -d  /etcc ] && echo "为真"  || echo  "为假"
为真


[root@shell /scripts/shell04]# cat if-1.sh
#!/bin/bash
read -p "请输入你要测试的文件:" File
if [ -f $File ];then
    echo "$File 文件存在"
else
    echo "$File 文件不存在"
fi
[root@shell /scripts/shell04]# sh if-1.sh
请输入你要测试的文件:/etc/hosts
/etc/hosts 文件存在
[root@shell /scripts/shell04]# sh if-1.sh
请输入你要测试的文件:/etc
/etc 文件不存在
[root@shell /scripts/shell04]# sh if-1.sh
请输入你要测试的文件:/etc/hostss
/etc/hostss 文件不存在

[root@shell /scripts/shell04]# cat if-2.sh
#!/bin/bash
read -p "请输入你要测试的目录:" Dir
if [  ! -d $Dir ];then
    echo "$Dir 目录不存在"
else
    echo "$Dir 目录存在"
fi
[root@shell /scripts/shell04]# sh if-2.sh
请输入你要测试的目录:/etc/
/etc/ 目录存在
[root@shell /scripts/shell04]# sh if-2.sh
请输入你要测试的目录:/etcc/
/etcc/ 目录不存在

#案例 

备份数据库

1. 怎么备份数据库        mysqldump  -uroot -p123 -B db_name > mysql.sql  

2. 备份到哪里         /backup/mysql

3. 提示用户输入要备份的数据库 read -p  

4. 备份之后的数据库名称  时间属性信息+数据库名称.sql

[root@shell /scripts/shell04]# cat if-3.sh
#!/bin/bash
#1.定义相关变量
Back_Dir=/backup/mysql
M_User=root
M_Pass=123
Date=$(date +%F)
#2.判读备份目录是否存在,不存在则创建
[ -d $Back_Dir ] || mkdir -p $Back_Dir
#3.提示用户输入要备份的目录
read -p "请输入你要备份的数据库名称:" Db_Name
#4.根据用户输入的数据库名称进行备份
mysqldump -u$M_User -p$M_Pass -B $Db_Name > $Back_Dir/${Db_Name}_${Date}.sql #备份数据的命令后面千万不要将执行结果定向到空!
#5.根据备份的结果进行判断
if [ $? -eq 0 ];then
    echo "数据库$Db_Name 备份成功................."
else
    echo "数据库$Db_Name 备份失败................."
fi

[root@shell /scripts/shell04]# 
[root@shell /scripts/shell04]# sh if-3.sh
请输入你要备份的数据库名称:wordpress
数据库wordpress 备份成功.................
[root@shell /scripts/shell04]# ll /backup/mysql/
total 452
-rw-r--r-- 1 root root 462530 2020-02-24 10:10 wordpress_2020-02-24.sql
[root@shell /scripts/shell04]# sh if-3.sh
请输入你要备份的数据库名称:world
数据库world 备份成功.................
[root@shell /scripts/shell04]# ll /backup/mysql/
total 692
-rw-r--r-- 1 root root 462530 2020-02-24 10:10 wordpress_2020-02-24.sql
-rw-r--r-- 1 root root 244162 2020-02-24 10:11 world_2020-02-24.sql
[root@shell /scripts/shell04]# sh if-3.sh
请输入你要备份的数据库名称:bucunzai
mysqldump: Got error: 1049: "Unknown database 'bucunzai'" when selecting the database
数据库bucunzai 备份失败.................


#严谨版备份脚本
[root@shell /scripts/shell04]# cat if-4.sh
#!/bin/bash
#1.定义相关变量
Back_Dir=/backup/mysql
Date=$(date +%F)
#2.判读备份目录是否存在,不存在则创建
[ -d $Back_Dir ] || mkdir -p $Back_Dir
#3.提示用户输入要备份的目录
read -p "请输入你要备份数据库的账户名称:" M_User
read -s -p "请输入${M_User}数据库账户的密码:" M_Pass
echo
read -p "请输入你要备份的数据库名称:" Db_Name
#4.根据用户输入的数据库名称进行备份
mysqldump -u$M_User -p$M_Pass -B $Db_Name > $Back_Dir/${Db_Name}_${Date}.sql #备份数据的命令后面千万不要将执行结果定向到空!
#5.根据备份的结果进行判断
if [ $? -eq 0 ];then
    echo "数据库$Db_Name 备份成功................."
else
    echo "数据库$Db_Name 备份失败................."
fi
[root@shell /scripts/shell04]# sh if-4.sh
请输入你要备份数据库的账户名称:root
请输入root数据库账户的密码:
请输入你要备份的数据库名称:wordpress
数据库wordpress 备份成功.................
[root@shell /scripts/shell04]# ll /backup/mysql/
total 452
-rw-r--r-- 1 root root 462530 2020-02-24 10:30 wordpress_2020-02-24.sql

3. 流程控制语句之整数比较

-eq            等于条件则为真           [ 1 -eq 1 ]

-ne            不等于                     [ 1 -ne 2 ]

-gt            大于                      [ 2 -gt 1 ]

-ge            大于等于                 [ 2 -ge 2 ]

-lt            小于                      [ 1 -lt 2 ]

-le         小于等于                 [ 1 -le 1 ]

#示例

[root@shell /scripts/shell04]# [ 1 -eq 1 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 2 -eq 1 ] && echo "为真"  || echo "为假"
为假
[root@shell /scripts/shell04]# [ 2 -ne 1 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 1 -ne 1 ] && echo "为真"  || echo "为假"
为假
[root@shell /scripts/shell04]# [ 1 -gt 1 ] && echo "为真"  || echo "为假"
为假
[root@shell /scripts/shell04]# [ 2 -gt 1 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 1 -ge 1 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 3 -ge 1 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 3 -lt 1 ] && echo "为真"  || echo "为假"
为假
[root@shell /scripts/shell04]# [ 1 -lt 1 ] && echo "为真"  || echo "为假"
为假
[root@shell /scripts/shell04]# [ 1 -lt 2 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 1 -le 2 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 2 -le 2 ] && echo "为真"  || echo "为假"
为真
[root@shell /scripts/shell04]# [ 2 -le 3 ] && echo "为真"  || echo "为假"
为真

#脚本示例

[root@shell /scripts/shell04]# cat if-5.sh
#!/bin/bash
read -p "请输入你要进行比较的第一个数字:" Num1
read -p "请输入你要进行比较的第二个数字:" Num2
if [ $Num1 -eq $Num2 ];then
    echo "$Num1 = $Num2"
elif [ $Num1 -gt $Num2 ];then
    echo "$Num1 > $Num2"
elif [ $Num1 -lt $Num2 ];then
    echo "$Num1 < $Num2"
else
    echo "你输入的数字无法进行比较!"
fi
[root@shell /scripts/shell04]# sh if-5.sh
请输入你要进行比较的第一个数字:10
请输入你要进行比较的第二个数字:10
10 = 10
[root@shell /scripts/shell04]# sh if-5.sh
请输入你要进行比较的第一个数字:10
请输入你要进行比较的第二个数字:20
10 < 20
[root@shell /scripts/shell04]# sh if-5.sh
请输入你要进行比较的第一个数字:20
请输入你要进行比较的第二个数字:10
20 > 10
[root@shell /scripts/shell04]# sh if-5.sh
请输入你要进行比较的第一个数字:20.5
请输入你要进行比较的第二个数字:10
if-5.sh: line 4: [: 20.5: integer expression expected
if-5.sh: line 6: [: 20.5: integer expression expected
if-5.sh: line 8: [: 20.5: integer expression expected
你输入的数字无法进行比较!

#实例

编写一个脚本,检测一个服务是否在运行状态

    1. 怎么判断一个服务是否在运行中  systemctl  status sshd

        当服务在运行状态下  返回值是0

        当服务没有在运行状态下 返回值是3

        当系统没有这个服务    返回值是4

        当输入的服务名称是数字时  返回值是1或者其他

    注意:当使用多分支结构的时候,$?不能直接使用,建议将其定义成一个变量

[root@shell /scripts/shell04]# cat if-6.sh
#!/bin/bash
#1.提示用户输入一个要进行检测的服务名称
read -p "请输入一个你要检测的服务名称:" Server
#2.查看这个服务是否在运行中
systemctl status  $Server &>/dev/null
#3.根据服务的返回值进行判断
Rc=$?
if [ $Rc -eq 0 ];then
    echo "$Server 服务正在运行中................."
elif [ $Rc -eq 3 ];then
    echo "$Server 没有在运行中..................."
elif [ $Rc -eq 4 ];then
    echo "$Server 没有这个服务!"
else
    echo "系统运行中的所有服务没有这个PID号存在!"
fi

[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:sshd
sshd 服务正在运行中.................
[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:nginx
nginx 没有这个服务!
[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:mariadb
mariadb 服务正在运行中.................
[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:nfs
nfs 没有在运行中...................
[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:12345
系统运行中的所有服务没有这个PID号存在!!
[root@shell /scripts/shell04]# sh if-6.sh
请输入一个你要检测的服务名称:18310
18310 服务正在运行中.................

2. 查看磁盘根分区的使用率,如果使用大于80%则进行报警发邮件

    1. 怎么查看磁盘/分区的使用率

    2. 怎么把这个使用率提取出来

    3. 根据使用率的大小进行判断, 整数  如何得到使用率的整数

    4. 调用函数库或者使用echo输出颜色

    5. 写完之后,把执行的结果截图发出来


[root@shell /scripts/shell04]# cat disk_used.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#1.定义磁盘使用率的大小变量
Disk_Used=$(df -h | awk '/\/$/{print $(NF-1)}')
#2. 拿磁盘的使用率大小跟80进行比较
if [ ${Disk_Used/\%/} -gt 10 ];then
    action "当前磁盘根分区使用率过高!使用率为:${Disk_Used}" /bin/false
else
    action "当前磁盘根分区使用率正常!使用率为:${Disk_Used}" /bin/true
fi
[root@shell /scripts/shell04]# sh disk_used.sh
当前磁盘根分区使用率过高!使用率为:11%                     [FAILED]
[root@shell /scripts/shell04]# sh disk_used.sh
当前磁盘根分区使用率正常!使用率为:5%                      [  OK  ]

3.写一个脚本,进行创建用户

    1. 提示用户输入要创建的用户

    2. 判断这个用户是否存在

    3. 用户不存在时,进行创建用户  

    4. 判断用户是否创建成功!给予提示


[root@shell /scripts/shell04]# cat if-7.sh
#!/bin/bash
#1.提示用户输入要创建的用户
read -p "请输入你要创建的用户:" User
#2.判断要进行新创建的用户是否存在
id $User &>/dev/null
if [ $? -eq 0 ];then
    echo "用户$User 已经存在!"
else
    #3.不存在则进行创建用户
    useradd $User &>/dev/null
    #4.判断用户是否创建成功
    if [ $? -eq 0 ];then
        echo "用户$User 创建成功!"
    else
        echo "用户$User 创建失败!"
    fi
fi
[root@shell /scripts/shell04]# sh if-7.sh
请输入你要创建的用户:test
用户test 创建成功!
[root@shell /scripts/shell04]# sh if-7.sh
请输入你要创建的用户:test
用户test 已经存在!

4. 测试URL地址是否能够访问

    1. 调用函数库

    2. 提示用户输入一个URL地址

    3. 测试该URL地址是否通畅

    4. 根据测试的结果进行返回内容

[root@shell /scripts/shell04]# cat if-8.sh
#!/bin/bash
#1.调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件/etc/init.d/functions不存在!"
#2.提示用户输入要进行测试的URL地址
read -p "请输入你要进行测试的URL地址:" Url
#3. 将用户输入好的URL地址进行网络测试
ping -c1 -W1 $Url  &>/dev/null
#4.根据测试结果进行返回内容
if [ $? -eq 0 ];then
    action "$Url 地址是可以进行连通的................" /bin/true
else
    action "$Url 地址是不可以进行连通的.............." /bin/false
fi
[root@shell /scripts/shell04]# sh if-8.sh
请输入你要进行测试的URL地址:www.baidu.com
www.baidu.com 地址是可以进行连通的................         [  OK  ]
[root@shell /scripts/shell04]# sh if-8.sh
请输入你要进行测试的URL地址:www.baiduy.com
www.baiduy.com 地址是不可以进行连通的..............        [FAILED]

4. 流程控制语句之字符比较

==        等于则条件为真      [ "a" == "a" ]

!=        不等于则条件为真     [ "a" != "b" ]

-z        字符串的长度为零则为真    

-n        字符串的长度不为零则为真

[root@shell /scripts/shell04]# [ "$USER" == "root" ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [ "$USER" == "roo" ] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [ "$USER" != "roo" ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [ "$USER" != "root" ] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# name=123
[root@shell /scripts/shell04]# echo ${#name}
3
[root@shell /scripts/shell04]# [ -z $name ] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# name=""
[root@shell /scripts/shell04]# echo ${#name}
0
[root@shell /scripts/shell04]# [ -z $name ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# name=123
[root@shell /scripts/shell04]# [ -z $name ] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [ -n $name ] && echo "为真" || echo "为假"
为真

[root@shell /scripts/shell04]# cat if-9.sh
#!/bin/bash
#1.提示用户输入一个yes
read -p "请输入一个yes:" Yes
if [ "$Yes" == "yes" ];then
    echo "你输入的是个yes"
else
    echo "你输入的不是个yes"
fi
#2.请输入一个除空值以外的字符
read -p "请输入一个除空值以外的字符:" Null
if [ -z $Null ];then
    echo "你输入的是一个空值!不允许直接敲回车键!"
else
    echo "你输入的是一个正常的值!"
fi
[root@shell /scripts/shell04]# sh if-9.sh
请输入一个yes:yes
你输入的是个yes
请输入一个除空值以外的字符:huirg
你输入的是一个正常的值!
[root@shell /scripts/shell04]# sh if-9.sh
请输入一个yes:yes
你输入的是个yes
请输入一个除空值以外的字符:
你输入的是一个空值!不允许直接敲回车键!

5. 流程控制语句之多条件比较

-a   并且    当多个条件存在时,必须是条件成立才为真,如果有一个条件不为真时,则条件为假

-o   或者       当多个条件存在时,必须是所有条件不为真时则为假,如果有一个条件为真时,则整体条件为真

[root@shell /scripts/shell04]# [ 1 -lt 2 -a 2 -gt 3 ] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [ 1 -lt 2 -a 4 -gt 3 ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [ 1 -lt 2 -o 4 -gt 3 ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [ 3 -lt 2 -o 4 -gt 3 ] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [ 3 -lt 2 -o 2 -gt 3 ] && echo "为真" || echo "为假"
为假


使用下面两个符号作为条件比对,必须使用双中括号

&&   并且  


||    或者

[root@shell /scripts/shell04]# [ 3 -lt 2 ||  2 -gt 3 ] && echo "为真" || echo "为假"
-bash: [: missing `]'
-bash: 2: command not found
为假
[root@shell /scripts/shell04]# [[ 3 -lt 2 ||  2 -gt 3 ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [[ 1 -lt 2 ||  2 -gt 3 ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [[ 1 -lt 2 &&  2 -gt 3 ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [[ 1 -lt 2 &&  4 -gt 3 ]] && echo "为真" || echo "为假"
为真


案例:根据学生的成绩,进行评价优劣

    0           地秀
    1-59         不及格
    60-79        良好
    80-99        优秀
    100            天秀


    1. 提示用户输入一个分数

    2. 判断用户输入是否空值或者是数字

    3. 根据用户输入的数字进行判读成绩的优劣


#单条件


[root@shell /scripts/shell04]# cat if-10.sh
#!/bin/bash
#1.提示用户输入一个分数
read -p "请输入你的成绩:" Fs
#2.判断用户输入的成绩是否是空值
if [ -z $Fs ];then
    echo "不支持直接回车!"
    exit
fi
#3.判读用户输入是否是数字或者是整数
expr 1 + $Fs &>/dev/null
#4.根据计算结果进行判断
if [ $? -ne 0 ];then
    echo "你输入的不是一个整数!"
    exit
fi
#5.根据用户输入的整数进行判断成绩的优劣
if [ $Fs -eq 0 ];then
    echo "你是个人才!给你评价为地秀!"
elif [ $Fs -lt 60 ];then
    echo "成绩不及格!需要补考!"
elif [ $Fs -lt 80 ];then
    echo "成绩良好!需要再接再厉!"
elif [ $Fs -lt 100 ];then
    echo "成绩优秀!但是没什么奖励!"
elif [ $Fs -eq 100 ];then
    echo "你很牛掰!封你为天秀!"
else
    echo "你很顽皮!请输入0-100之间的数字!"
fi
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:
不支持直接回车!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:a
你输入的不是一个整数!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:1.5
你输入的不是一个整数!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:0
你是个人才!给你评价为地秀!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:59
成绩不及格!需要补考!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:79
成绩良好!需要再接再厉!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:99
成绩优秀!但是没什么奖励!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:100
你很牛掰!封你为天秀!
[root@shell /scripts/shell04]# sh if-10.sh
请输入你的成绩:1000
你很顽皮!请输入0-100之间的数字!



#多条件
#!/bin/bash
##############################################################
# File Name: fenshu.sh
# Time: 2020-02-24-16:23:59
# Author: 高程程
##############################################################
while : 
do
#1.输入测试分数
read -p "请输入你分数:" num

#2.判断是否有直接回车
if [ -z $num ];then
    echo "请输入正确数字,不可以直接回车"
    exit
fi

#3.判断用户是否输入数字或者整数
expr  1 + $num &>/dev/null
if [ $? -ne 0 ];then
    echo "您输入的不是一个整数"
    exit                                                                      
fi

#4.根据用户输入的分数进行匹配
if [ $num -eq 0 ];then
    echo "${num}分数属于地秀"
elif [ $num -gt 0 -a $num -lt 60 ];then
    echo "${num}属于不及格"
elif [ $num -gt 59 -a $num -lt 70 ];then
    echo "${num}属于及格"
elif [ $num -gt 69 -a $num -lt 100 ];then
    echo "${num}分数属于良好"
elif [ $num -eq 100 ];then
    echo "${num}分数属于天秀"
else
    echo "请输入0-100"
fi
done

6. 流程控制语句之正则比较

[root@shell /scripts/shell04]# [ "$USER" =~ "^r" ] && echo "为真" || echo "为假"
-bash: [: =~: binary operator expected
为假
[root@shell /scripts/shell04]# [[ "$USER" =~ "^r" ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [[ "$USER" =~ ^r ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [[ "$USER" =~ '^r' ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# Name=rtoo
[root@shell /scripts/shell04]# [[ "$Name" =~ '^r' ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# [[ "$Name" =~ ^r ]] && echo "为真" || echo "为假"
为真

#在进行正则比较的时候,必须使用双中括号,正则比较,除非是变量需要使用双引号引起来,其他的值一律不允许使用引号

[root@shell /scripts/shell04]# echo $Name
rtoo
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z] ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# Name=abc1
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z] ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z]*$ ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# Name=abc
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z]*$ ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# Name=""
[root@shell /scripts/shell04]# echo $Name

[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z]*$ ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# # * 匹配前面的字符出现0次或者0次以上
[root@shell /scripts/shell04]# # + 匹配前面的字符出现1次或者1次以上
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[a-Z]+$ ]] && echo "为真" || echo "为假"
为假


[root@shell /scripts/shell04]# Name=123
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[0-9]+$ ]] && echo "为真" || echo "为假"
为真
[root@shell /scripts/shell04]# Name=123a
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[0-9]+$ ]] && echo "为真" || echo "为假"
为假
[root@shell /scripts/shell04]# Name=123.5
[root@shell /scripts/shell04]# [[ "$Name" =~ ^[0-9]+$ ]] && echo "为真" || echo "为假"
为假

#脚本示例

[root@shell /scripts/shell04]# cat if-12.sh
#!/bin/bash
read -p "请输入一个任意的英文单词:" En
if [[ $En =~ ^[a-Z]+$ ]];then
    echo "你输入的是一个英文单词"
else
    echo "你输入的英文单词不符合要求"
fi
read -p "请输入一个数字且是整数:" Num
if [[ $Num =~ ^[0-9]+$ ]];then
    echo "你输入的是一个数字且是整数!"
else
    echo "你输入的数字不符合要求"
fi
[root@shell /scripts/shell04]# sh if-12.sh
请输入一个任意的英文单词:abc1
你输入的英文单词不符合要求
请输入一个数字且是整数:123a
你输入的数字不符合要求
[root@shell /scripts/shell04]# sh if-12.sh
请输入一个任意的英文单词:num
你输入的是一个英文单词
请输入一个数字且是整数:234
你输入的是一个数字且是整数!

#脚本示例

创建用户脚本,用户名由字母和数字组成,字母作为用户的前缀,数字最为用户的后缀。 比如: oldboy123 

    1. 提示用户输入用户的前缀

    2. 判断用户的前缀是否是由字母组成的

    3. 提示用户输入后缀

    4. 判断用户的后缀是否是由数字组成的

    5. 将用户的前缀和后缀组合成一个完整的用户名

    6. 判断该用户是否存在该系统,存在则不创建

    7. 不存在则进行创建

    8. 判断用户是否创建成功

[root@shell /scripts/shell04]# cat useradd.sh
#!/bin/bash
#1.提示用户输入创建用户的前缀
read -p "请输入你要创建用户的前缀:" Qz
#2.判断前缀是否全部由数字组成
if [[ ! $Qz =~ ^[a-Z]+$ ]];then
    echo "你输入的前缀不符合要求!前缀必须是由字母组成的!"
    exit
fi
#3.提示用户输入要创建用户的后缀
read -p "请输入你要创建用户的后缀:" Hz
#4.判断用户输入的后缀是否由数字组成
if [[ ! $Hz =~ ^[0-9]+$ ]];then
    echo "你输入的后缀不符合要求!后缀必须是由数字组成的!"
    exit
fi
#5.将前缀和后缀进行组合在一起
User=${Qz}${Hz}
#6.判断此用户是否存在该系统
id $User &>/dev/null
#7.根据上条命令的执行结果进行输出结果
if [ $? -eq 0 ];then
    echo "用户$User 已存在该系统!"
else
    useradd $User &>/dev/null
    #8.判断用户是否创建成功
    if [ $? -eq 0 ];then
        echo "用户$User 创建成功..............."
    else
        echo "用户$User 创建失败..............."
    fi
fi
[root@shell /scripts/shell04]# sh useradd.sh
请输入你要创建用户的前缀:abc1
你输入的前缀不符合要求!前缀必须是由字母组成的!
[root@shell /scripts/shell04]# sh useradd.sh
请输入你要创建用户的前缀:abc
请输入你要创建用户的后缀:123a
你输入的后缀不符合要求!后缀必须是由数字组成的!
[root@shell /scripts/shell04]# sh useradd.sh
请输入你要创建用户的前缀:
你输入的前缀不符合要求!前缀必须是由字母组成的!
[root@shell /scripts/shell04]# sh useradd.sh
请输入你要创建用户的前缀:abc
请输入你要创建用户的后缀:1
用户abc1 创建成功...............
[root@shell /scripts/shell04]# sh useradd.sh
请输入你要创建用户的前缀:abc
请输入你要创建用户的后缀:1
用户abc1 已存在该系统!

7. 流程控制语句场景示例

1. 清空/var/log/messages日志,保留最近的100行内容,且只能是超级管理员root才有权限执行这个脚本

    1. 必须是root用户才有权限执行这个脚本

    2. 清空/var/log/messages日志,判断这个日志文件是否存在

    3. 清空之后还要保留最近的100行内容

[root@shell /scripts/shell05]# cat if-1.sh
#!/bin/bash
#1.判断当前的用户是否是超级管理员root用户
if [ "$USER" == "root" -a "$UID" -eq 0 ];then
    #2.判断/var/log/messages日志文件是否存在
    File=/var/log/messages
    if [ -f $File ];then
        tail -100 $File > ${File}.bak && mv -f  ${File}.bak $File  &>/dev/null
        #3.判断日志是否清空和保留成功
        if [ $? -eq 0 ];then
            echo "${File}日志文件清空成功!并且保留了最近的100行内容!"
        else
            echo "${File}日志文件清空失败!保留失败!"
        fi
    else
        echo "${File}日志文件不存在!"
    fi
else
    echo "${USER}用户没有权限执行该脚本!"
fi
[root@shell /scripts/shell05]# sh if-1.sh
/var/log/messages日志文件清空成功!并且保留了最近的100行内容!
[root@shell /scripts/shell05]# wc -l /var/log/messages
100 /var/log/messages
[root@shell /scripts/shell05]# ll /var/log/messages
-rw-r--r-- 1 root root 6833 2020-02-25 10:48 /var/log/messages

#第二种方法:将多个if语句分开写

2. 判断一个服务是否在正常运行   sshd

    1. 判断其是否在运行中  systemctl  status sshd

    2. 判断端口号是否存在  netstat  -lntp |grep sshd

    3. 判断服务进程是否存在  ps aux |grep sshd | grep -Ev 'grep|pts


[root@shell /scripts/shell05]# cat if-2.sh
#!/bin/bash
#1.判断脚本的位置变量是否只有一个
if [ $# -ne 1 ];then
    echo "Usage:$0 {sshd|nginx|mariadb|nfs}"
    exit
fi
#2.0. 判断服务是否是rsync
if [ $1 == "rsync" ];then
    Server1=rsyncd
else
    Server1=$1
fi
#2.判断服务是否在运行中
systemctl  status  ${Server1}  &>/dev/null
#3.根据命令的执行结果进行判断服务在什么状态
Rc=$?
if [ $Rc -eq 0 ];then
    echo "${1}服务正在运行中................."
elif [ $Rc -eq 3 ];then
    echo "${1}服务没有运行中................."
elif [ $Rc -eq 4 ];then
    echo "系统中没有安装这个${1}服务!"
    exit
else
    echo "当前系统中没有正在运行的服务进程ID为$1"
    exit
fi
#4.0.判断服务是否是mariadb
if [ $1 == "mariadb" ];then
    Server=mysqld
else
    Server=$1
fi
#4.判断服务端口号是否存在
netstat -lntp | grep $Server  &>/dev/null
#5.根据命令的执行结果进行判断服务是否存在端口号
if [ $? -eq 0 ];then
    echo "${1}服务端口号存在............."
else
    echo "${1}服务端口号不存在..........."
fi
#6.判断服务的进程是否存在
ps aux | grep $1 | grep -Ev 'grep|pts'  &>/dev/null
#7.根据命令的执行结果进行判断服务是否存在进程
if [ $? -eq 0 ];then
    echo "${1}服务进程存在................."
else
    echo "${1}服务进程不存在..............."
fi
[root@shell /scripts/shell05]# sh if-2.sh rsync
rsync服务正在运行中.................
rsync服务端口号存在.............
rsync服务进程存在.................
[root@shell /scripts/shell05]# sh if-2.sh mariadb
mariadb服务正在运行中.................
mariadb服务端口号存在.............
mariadb服务进程存在.................
[root@shell /scripts/shell05]# sh if-2.sh sshd
sshd服务正在运行中.................
sshd服务端口号存在.............
sshd服务进程存在.................

3. 根据不同的操作系统安装不同的yum源仓库

    centos  7.6 

    centos    6.x  

    1. 获取到当前系统的版本

    2. 根据系统的版本进行安装对应的yum源

    3. 下载更新Base和Epel源

    4. 判断当前系统的网络情况

    5. 更新源之后,在判断是否更新成功

[root@shell /scripts/shell05]# cat if-3.sh
#!/bin/bash
#1.定义系统版本变量
Version=$(awk '{print $(NF-1)}'  /etc/redhat-release)
#2.根据版本信息进行下载更新不同的yum源
if [ ${Version%%.*} -eq 6 ];then
    echo "当前操作系统版本为CentOS-6系列。"
    #3.判断当前系统的网络状况
    ping -c1 -W1 www.baidu.com &>/dev/null
    #4.根据网络状况进行判断
    if [ $? -eq 0 ];then
        #5.移除当前系统中的旧的Yum源
        rm -rf /etc/yum.repos.d/* && echo "当前系统旧的yum源移除成功!" || echo "当前系统旧的yum源移除失败!" 
        #6.下载更新Base源
        echo "正在下载更新Base源................."
        curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && echo "Base源下载更新成功!" || echo "Base源下载更新失败!" 
        #7.下载更新Epel源
        echo "正在下载更新Epel源................."
        curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo && echo "Epel源下载更新成功!" || echo "Epel源下载更新失败!"
        echo "CentOS-6系列的yum源仓库更新成功!"
    else
        echo "当前操作系统的网络状况不佳!无法进行下载更新yum源!"
        exit
    fi
elif [ ${Version%%.*} -eq 7 ];then
    echo "当前操作系统版本为CentOS-7系列。"
    #3.判断当前系统的网络状况
    ping -c1 -W1 www.baidu.com &>/dev/null
    #4.根据网络状况进行判断
    if [ $? -eq 0 ];then
        #5.移除当前系统中的旧的Yum源
        rm -rf /etc/yum.repos.d/* && echo "当前系统旧的yum源移除成功!" || echo "当前系统旧的yum源移除失败!" 
        #6.下载更新Base源
        echo "正在下载更新Base源................."
        curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && echo "Base源下载更新成功!" || echo "Base源下载更新失败>!" 
        #7.下载更新Epel源
        echo "正在下载更新Epel源................."
        curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && echo "Epel源下载更新成功!" || echo "Epel源下载更新失败!"
        echo "CentOS-6系列的yum源仓库更新成功!"
    else
        echo "当前操作系统的网络状况不佳!无法进行下载更新yum源!"
        exit
    fi
else
    echo "当前操作系统的版本没有可准备的yum源使用!"
    exit
fi
[root@shell /scripts/shell05]# sh if-3.sh
当前操作系统版本为CentOS-7系列。
当前系统旧的yum源移除成功!
正在下载更新Base源.................
Base源下载更新成功!
正在下载更新Epel源.................
Epel源下载更新成功!
CentOS-6系列的yum源仓库更新成功!

8. 流程控制语句之case语句

case主要作用是对程序的选择,循环等操作


#语法:

case 变量  in
    变量值1)
            命令序列
            ;;
    变量值2)
            命令序列
            ;;
    变量值3)
            命令序列
            ;;
    变量值N)
            命令序列
            ;;
    *)
            命令序列
            exit
esac


#语法示例

安装不同PHP的版本

    1. 菜单,PHP版本的菜单

    2. 提示用户根据菜单进行选择安装不同的PHP版本

    3. 根据用户选择进行安装不同的PHP版本

[root@shell /scripts]# cat case-1.sh
#!/bin/bash
#1.准备PHP的版本菜单
cat<<EOF
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
EOF
#2.提示用户根据菜单进行选择安装不同的PHP版本
read -p "请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:" Install
#3.根据用户的选择,进行安装不同的版本
case $Install  in
    1)
        echo "你选择了安装PHP-5.5版本..............."
        echo "正在安装PHP-5.5版本,请稍后............"
        sleep 3
        echo "PHP-5.5版本安装成功..................."
        ;;
    2)
        echo "你选择了安装PHP-5.6版本..............."
        echo "正在安装PHP-5.6版本,请稍后............"
        sleep 3
        echo "PHP-5.6版本安装成功..................."
        ;;
    3)
        echo "你选择了安装PHP-7.1版本..............."
        echo "正在安装PHP-7.1版本,请稍后............"
        sleep 3
        echo "PHP-7.1版本安装成功..................."
        ;;
    4)
        echo "你没有选择安装任何PHP版本!程序退出!"
        exit
        ;;
    *)
        echo "你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4]."
        exit
esac
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:5
你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4].
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:a
你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4].
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:m
你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4].
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:1
你选择了安装PHP-5.5版本...............
正在安装PHP-5.5版本,请稍后............
PHP-5.5版本安装成功...................
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:2
你选择了安装PHP-5.6版本...............
正在安装PHP-5.6版本,请稍后............
PHP-5.6版本安装成功...................
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:3
你选择了安装PHP-7.1版本...............
正在安装PHP-7.1版本,请稍后............
PHP-7.1版本安装成功...................
[root@shell /scripts]# sh case-1.sh
#############################
1. 安装PHP-5.5版本
2. 安装PHP-5.6版本
3. 安装PHP-7.1版本
4. 退出当前安装
#############################
请根据的上方的菜单进行选择安装不同的PHP版本[1|2|3|4]:4
你没有选择安装任何PHP版本!程序退出!

9. case语句之场景示例

1. 编写一个rsync的启动停止脚本

    实现:  start  stop status restart

    1. 如何启动rsync  

    /usr/bin/rsync --daemon 

    2. 如何停止rsync

    pkill  rsync   #注意:千万不要使用rsync作为脚本的名字

    3. 参考系统中其他的服务的pid文件,我们自己创建


[root@shell /scripts]# cat case-2.sh
#!/bin/bash
#1.引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量的个数是否只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart}"
    exit
fi
#3.编写case语句选项
Pid_File=/var/run/rsync.pid
case $1 in
    start)
            if [ -f $Pid_File ];then
                action "服务Rsync正在运行中..............."  /bin/true
                exit
            else
                #当服务要启动的时候,需要手动创建pid文件
                touch $Pid_File  && /usr/bin/rsync --daemon &>/dev/null && sleep 3
                if [ $? -eq 0 ];then
                    action "服务Rsync启动成功................"  /bin/true
                else
                    action "服务Rsync启动失败................" /bin/false
                fi
            fi
            ;;
    stop)
            if [ -f $Pid_File ];then
                #当服务在启动的状态下,pid文件是存在的,如果需要停止服务,同样需要把pid文件删除
                rm -f $Pid_File && pkill rsync && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Rsync停止成功............." /bin/true
                else
                    action "服务Rsync停止失败............." /bin/false
                fi
            else
                action "服务Rsync不在运行中..............." /bin/true
                exit
            fi
            ;;
    status)
            #当pid文件存在时,则服务在运行中,pid文件不存在时,服务不在运行中
            if [ -f $Pid_File ];then
                action "服务Rsync正在运行中................" /bin/true
            else
                action "服务Rsync不在运行中................" /bin/true
            fi
            ;;
    restart)
            #重启,当服务在运行中,需要停止服务再启动服务 ,当服务不在运行中,需要直接启动
            if [ -f $Pid_File ];then
                #停止服务
                rm -f $Pid_File && pkill rsync && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Rsync停止成功............." /bin/true
                else
                    action "服务Rsync停止失败............." /bin/false
                fi
                #启动服务
                touch $Pid_File  && /usr/bin/rsync --daemon &>/dev/null && sleep 3
                if [ $? -eq 0 ];then
                    action "服务Rsync启动成功................"  /bin/true
                else
                    action "服务Rsync启动失败................" /bin/false
                fi
                action "服务Rsync重启成功...................." /bin/true
            else
                action "服务Rsync不在运行中.................." /bin/true
                #启动服务
                touch $Pid_File  && /usr/bin/rsync --daemon &>/dev/null && sleep 3
                if [ $? -eq 0 ];then
                    action "服务Rsync启动成功................"  /bin/true
                else
                    action "服务Rsync启动失败................" /bin/false
                fi
                action "服务Rsync重启成功...................." /bin/true
            fi
            ;;
    *)
            echo "Usage: $0 {start|stop|status|restart}"
            exit
esac

[root@shell /scripts]# sh case-2.sh start
服务Rsync正在运行中...............                         [  OK  ]
[root@shell /scripts]# sh case-2.sh stop
服务Rsync停止成功.............                             [  OK  ]
[root@shell /scripts]# ps aux | grep rsync
root      24687  0.0  0.0 112708   976 pts/2    R+   10:49   0:00 grep --color=auto rsync

10. Nginx的启动停止脚本

    start  stop  status  restart  reload

1. 怎么启动Nginx  

/usr/sbin/nginx

2. 怎么停止Nginx

/usr/sbin/nginx  -s stop

3. 怎么进行重启Nginx

先stop 在start

4. 怎么进行平滑重启Nginx

/usr/sbin/nginx  -s  reload

5. 在什么情况下,才能进行平滑重启

    只有在Nginx服务在运行的时候,才能进行平滑重启。如果Nginx服务没有在运行中,是不能进行平滑重启的。

#简单版Nginx服务启动脚本

[root@shell /scripts]# cat case-3.sh
#!/bin/bash
#1.引用函数库
[  -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量是否存在且只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit
fi
#3.编写case语句
Pid_File=/var/run/nginx.pid
case $1 in
    start)
            if [ -f $Pid_File ];then
                action "Nginx服务正在运行中..............."  /bin/true
            else
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功............." /bin/true
                else
                    action "Nginx服务启动失败............." /bin/false
                fi
            fi
            ;;
    stop)
            if [ -f $Pid_File ];then
                /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务停止成功.............." /bin/true
                else
                    action "Nginx服务停止失败.............." /bin/false
                fi
            else
                action "Nginx服务不在运行中................" /bin/true
            fi
            ;;
    status)
            if [ -f $Pid_File ];then
                action "Nginx服务正在运行中................" /bin/true
            else
                action "Nginx服务不在运行中................" /bin/true
            fi
            ;;
    restart)
            if [ -f $Pid_File ];then
                #先停止服务
                /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务停止成功.............." /bin/true
                else
                    action "Nginx服务停止失败.............." /bin/false
                fi
                #启动服务
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功.............." /bin/true
                    action "Nginx服务重启成功.............." /bin/true
                else
                    action "Nginx服务启动失败.............." /bin/false
                fi
            else
                action "Nginx服务不在运行中................" /bin/true
                #启动服务
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功.............." /bin/true
                    action "Nginx服务重启成功.............." /bin/true
                else
                    action "Nginx服务启动失败.............." /bin/false
                fi
            fi
            ;;
    reload)
            if [ -f $Pid_File ];then
                /usr/sbin/nginx -s reload &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务平滑重启成功........." /bin/true
                else
                    action "Nginx服务平滑重启失败........." /bin/false
                fi
            else
                action "Nginx服务没有在运行中,无法进行平滑重启....."  /bin/false
            fi
            ;;
    *)
            echo "Usage: $0 {start|stop|status|restart|reload}"
            exit
esac
[root@shell /scripts]# sh case-3.sh 
Usage: case-3.sh {start|stop|status|restart|reload}
[root@shell /scripts]# sh case-3.sh status
Nginx服务不在运行中................                        [  OK  ]
[root@shell /scripts]# sh case-3.sh start
Nginx服务启动成功.............                             [  OK  ]
[root@shell /scripts]# sh case-3.sh start
Nginx服务正在运行中...............                         [  OK  ]
[root@shell /scripts]# sh case-3.sh start
Nginx服务正在运行中...............                         [  OK  ]
[root@shell /scripts]# sh case-3.sh stop
Nginx服务停止成功..............                            [  OK  ]
[root@shell /scripts]# sh case-3.sh restart
Nginx服务不在运行中................                        [  OK  ]
Nginx服务启动成功..............                            [  OK  ]
Nginx服务重启成功..............                            [  OK  ]
[root@shell /scripts]# sh case-3.sh reload
Nginx服务平滑重启成功.........                             [  OK  ]
[root@shell /scripts]# sh case-3.sh stop
Nginx服务停止成功..............                            [  OK  ]
[root@shell /scripts]# sh case-3.sh reload
Nginx服务没有在运行中,无法进行平滑重启.....                [FAILED]


#加锁机制

当这个脚本被某一个人执行的时候,别人是无法执行的,只能等待执行结束之后,才能执行

[root@shell /scripts]# cat case-3.sh
#!/bin/bash
#1.引用函数库
[  -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量是否存在且只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit
fi
#2.5 进行加锁机制
Suo_File=/tmp/nginx.lock
if [ -f $Suo_File ];then
    echo "此脚本$0 正在运行中,请稍后再执行.............."
    exit
fi
#2.6 加锁
touch $Suo_File &>/dev/null
#3.编写case语句
Pid_File=/var/run/nginx.pid
case $1 in
    start)
            if [ -f $Pid_File ];then
                action "Nginx服务正在运行中..............."  /bin/true
            else
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功............." /bin/true
                else
                    action "Nginx服务启动失败............." /bin/false
                fi
            fi
            ;;
    stop)
            if [ -f $Pid_File ];then
                /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务停止成功.............." /bin/true
                else
                    action "Nginx服务停止失败.............." /bin/false
                fi
            else
                action "Nginx服务不在运行中................" /bin/true
            fi
            ;;
    status)
            if [ -f $Pid_File ];then
                action "Nginx服务正在运行中................" /bin/true
            else
                action "Nginx服务不在运行中................" /bin/true
            fi
            ;;
    restart)
            if [ -f $Pid_File ];then
                #先停止服务
                /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务停止成功.............." /bin/true
                else
                    action "Nginx服务停止失败.............." /bin/false
                fi
                #启动服务
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功.............." /bin/true
                    action "Nginx服务重启成功.............." /bin/true
                else
                    action "Nginx服务启动失败.............." /bin/false
                fi
            else
                action "Nginx服务不在运行中................" /bin/true
                #启动服务
                /usr/sbin/nginx  &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务启动成功.............." /bin/true
                    action "Nginx服务重启成功.............." /bin/true
                else
                    action "Nginx服务启动失败.............." /bin/false
                fi
            fi
            ;;
    reload)
            if [ -f $Pid_File ];then
                /usr/sbin/nginx -s reload &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "Nginx服务平滑重启成功........." /bin/true
                else
                    action "Nginx服务平滑重启失败........." /bin/false
                fi
            else
                action "Nginx服务没有在运行中,无法进行平滑重启....."  /bin/false
            fi
            ;;
    *)
            echo "Usage: $0 {start|stop|status|restart|reload}"
esac
#4. 解锁
rm -f $Suo_File &>/dev/null


[root@shell /scripts]# sh case-3.sh restart 
Nginx服务停止成功..............                            [  OK  ]
Nginx服务启动成功..............                            [  OK  ]
Nginx服务重启成功..............                            [  OK  ]
#在另外一个窗口同时执行脚本
[root@shell /scripts]# sh case-3.sh  restart 
此脚本case-3.sh 正在运行中,请稍后再执行..............

## 升级版

    简单的实现自动化的解决nginx的故障

[root@shell /scripts]# cat case-4.sh
#!/bin/bash
##############################################################
# File Name: nginx.sh
# Time: 2020-02-26-14:22:47
# Author: 高程程
##############################################################
#调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#判位置变量是否存在
if [ $# -ne 1 ];then
    echo "输入{start|stop|status|restart|reload}"
    exit
fi
#定义加锁变量
suo=/tmp/nginx.sh.suo
#判断枷锁机制文件是否存在存在则退出
if [ -f $suo ];then
            echo "脚本正在使用中,请稍后"
            exit
fi
#执行到这里的时候生成枷锁文件
touch $suo
#利用/var/run/nginx.pid进行判断
wenjian=/var/run/nginx.pid
#输入的如果是start的时候
if [ $1 == "start" ];then
    if [ -f $wenjian ];then
        action "nginx服务是启动中的" /bin/true
    else
        #否则进行nginx-t语法检查
        /usr/sbin/nginx -t 2>/tmp/nginx.err && sleep 3
        #对的则正常操作
        if [ $? -eq 0 ];then
            /usr/sbin/nginx  &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功............" /bin/true
            else
                action "Nginx服务启动失败" /bin/false
            fi
        else
        #错的则进行修正或者退出
            action "nginx语法有问题" /bin/false
            Nginx_Err_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' /tmp/nginx.err)
            Nginx_Err_line=$(awk  -F '[ :]' 'NR==1{print $NF}' /tmp/nginx.err)
            /usr/sbin/nginx -t
            read -p "${Nginx_Err_File}此配置文件的第${Nginx_Err_line}行出错!是否进行修改[Y|N]: " Re
            case $Re in
                Y|yes|Yes|y)
                    vim  +$Nginx_Err_line $Nginx_Err_File
                    /usr/sbin/nginx -t &>/dev/null
                    if [ $? -eq 0 ];then
                        echo "配置文件修改成功!正在启动Nginx服务......."
                        /usr/sbin/nginx  &>/dev/null && sleep 2
                        if [ $? -eq 0 ];then
                            action "Nginx服务启动成功............." /bin/true
                        else
                            action "Nginx服务启动失败............." /bin/false
                        fi
                    else
                        echo "配置文件修改之后,语法还是检查错误!你还是手动进行修改吧!"
                    fi
                    ;;
                N|No|NO|n)
                    echo "那你可以选择手动修改,记得删除本文件的加锁文件$suo"
                    ;;
                *)
                    echo "输入不规范"
            esac
        fi
    fi
elif [ $1 == "stop" ];then
    if [ -f $wenjian ];then
        /usr/sbin/nginx -t 2>/tmp/nginx.err && sleep 3
        if [ $? -eq 0 ];then
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功............." /bin/true
            else
                action "Nginx服务停止失败............." /bin/false
            fi
        else
            action "nginx语法有问题" /bin/false
            Nginx_Err_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' /tmp/nginx.err)
            Nginx_Err_line=$(awk  -F '[ :]' 'NR==1{print $NF}' /tmp/nginx.err)
            /usr/sbin/nginx -t
            read -p "${Nginx_Err_File}此配置文件的第${Nginx_Err_line}行出错!是否进行修改[Y|N]: " Re
            case $Re in
                Y|yes|Yes|y)
                    vim  +$Nginx_Err_line $Nginx_Err_File
                    /usr/sbin/nginx -t &>/dev/null
                    if [ $? -eq 0 ];then
                        echo "配置文件修改成功!正在stop Nginx服务......."
                        /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                        if [ $? -eq 0 ];then
                            action "Nginx服务stop成功............." /bin/true
                        else
                            action "Nginx服务stop失败............." /bin/false
                        fi
                    else
                        echo "配置文件修改之后,语法还是检查错误!你还是手动进行修改吧!"
                    fi
                    ;;
                N|No|NO|n)
                    echo "那你可以选择手动修改,记得删除本文件的加锁文件$suo"
                    ;;
                *)
                    echo "输入不规范"
            esac
        fi
    else
        echo "nginx已经是停止状态中"
    fi
elif [ $1 == "status" ];then
    if [ -f $wenjian ];then
        action "nginx服务运行中" /bin/true
    else
        action "nginx服务是关闭的"  /bin/true
    fi
elif [ $1 == "restart" ];then
    if [ -f $wenjian ];then
        /usr/sbin/nginx -t 2>/tmp/nginx.err && sleep 3
        if [ $? -eq 0 ];then
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功............." /bin/true
                /usr/sbin/nginx &>/dev/null && sleep 3
                if [ $? -eq 0 ];then
                    action  "nginx服务重启成功.........." /bin/true
                else
                    action  "nginx服务重启失败" /bin/false
                fi
            else
                action "Nginx服务停止失败............." /bin/false
            fi
        else
            action "nginx语法有问题" /bin/false
            Nginx_Err_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' /tmp/nginx.err)
            Nginx_Err_line=$(awk  -F '[ :]' 'NR==1{print $NF}' /tmp/nginx.err)
            /usr/sbin/nginx -t
            read -p "${Nginx_Err_File}此配置文件的第${Nginx_Err_line}行出错!是否进行修改[Y|N]: " Re
            case $Re in
                Y|yes|Yes|y)
                    vim  +$Nginx_Err_line $Nginx_Err_File
                    /usr/sbin/nginx -t &>/dev/null
                    if [ $? -eq 0 ];then
                        echo "配置文件修改成功!正在stop Nginx服务......."
                        /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                        if [ $? -eq 0 ];then
                            action "Nginx服务stop成功............." /bin/true
                            /usr/sbin/nginx &>/dev/null && sleep 3
                            if [ $? -eq 0 ];then
                                action  "nginx服务重启成功" /bin/true
                            else
                                action  "nginx服务重启失败" /bin/false
                            fi
                        else
                            action "Nginx服务stop失败............." /bin/false
                        fi
                    else                   
                        echo "配置文件修改之后,语法还是检查错误!你还是手动进行修改吧!"
                    fi
                    ;;
                N|No|NO|n)
                    echo "那你可以选择手动修改,记得删除本文件的加锁文件$suo"
                    ;;
                *)
                    echo "输入不规范"
            esac
        fi
    else
        echo "nginx已经是停止状态中"
        /usr/sbin/nginx -t 2>/tmp/nginx.err && sleep 3
        if [ $? -eq 0 ];then
            /usr/sbin/nginx  &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务重启成功............" /bin/true
            else
                action "Nginx服务重启失败" /bin/false
            fi
        else
            action "nginx语法有问题" /bin/false
            Nginx_Err_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' /tmp/nginx.err)
            Nginx_Err_line=$(awk  -F '[ :]' 'NR==1{print $NF}' /tmp/nginx.err)
            /usr/sbin/nginx -t
            read -p "${Nginx_Err_File}此配置文件的第${Nginx_Err_line}行出错!是否进行修改[Y|N]: " Re
            case $Re in
                Y|yes|Yes|y)
                    vim  +$Nginx_Err_line $Nginx_Err_File
                    /usr/sbin/nginx -t &>/dev/null
                    if [ $? -eq 0 ];then
                        echo "配置文件修改成功!正在重启Nginx服务......."
                        /usr/sbin/nginx  &>/dev/null && sleep 2
                        if [ $? -eq 0 ];then
                            action "Nginx服务重启成功............." /bin/true
                        else
                            action "Nginx服务重启失败............." /bin/false       
                        fi
                    else
                        echo "配置文件修改之后,语法还是检查错误!你还是手动进行修改吧!"
                    fi
                    ;;
                N|No|NO|n)
                    echo "那你可以选择手动修改,记得删除本文件的加锁文件$suo"
                    ;;
                *)
                    echo "输入不规范"
            esac
        fi
    fi
elif [ $1 == "reload" ];then
    if [ -f $wenjian ];then
        /usr/sbin/nginx -t 2>/tmp/nginx.err && sleep 3
        if [ $? -eq 0 ];then
            /usr/sbin/nginx -s reload &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务平滑重启成功........." /bin/true
            else
                action "Nginx服务平滑重启失败........." /bin/false
            fi
        else
            action "nginx语法有问题" /bin/false
            Nginx_Err_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' /tmp/nginx.err)
            Nginx_Err_line=$(awk  -F '[ :]' 'NR==1{print $NF}' /tmp/nginx.err)
            /usr/sbin/nginx -t
            read -p "${Nginx_Err_File}此配置文件的第${Nginx_Err_line}行出错!是否进行修改[Y|N]: " Re
            case $Re in
                Y|yes|Yes|y)
                    vim  +$Nginx_Err_line $Nginx_Err_File
                    /usr/sbin/nginx -t &>/dev/null
                    if [ $? -eq 0 ];then
                        echo "配置文件修改成功!正在平滑重启Nginx服务......."
                        /usr/sbin/nginx -s reload &>/dev/null && sleep 2
                        if [ $? -eq 0 ];then
                            action "Nginx服务平滑重启成功............." /bin/true
                        else
                            action "Nginx服务平滑重启失败............." /bin/false       
                        fi
                    else
                        echo "配置文件修改之后,语法还是检查错误!你还是手动进行修改吧!"
                    fi
                    ;;
                N|No|NO|n)
                    echo "那你可以选择手动修改,记得删除本文件的加锁文件$suo"
                    ;;
                *)
                    echo "输入不规范"
            esac
        fi
    fi
else
    echo "输入不规范"
fi
rm -rf $suo

11. 实现系统管理工具箱

1. 实现系统管理工具箱

###########################
1. 显示磁盘的使用情况
2. 显示内存使用情况
3. 显示系统负载情况
4. 显示CPU使用情况
5. 查看系统Eth0网卡
6. 查看系统外网IP
7. 查看系统版本
8. 查看系统内核版本
9. 退出脚本程序
0. 显示帮助菜单
###########################

简单版的工具箱


[root@shell /scripts/shell07]# cat case-1.sh
#!/bin/bash
#1. 编写菜单
cat<<EOF
###########################
1. 显示磁盘的使用情况
2. 显示内存使用情况
3. 显示系统负载情况
4. 显示CPU使用情况
5. 查看系统Eth0网卡
6. 查看系统外网IP
7. 查看系统版本
8. 查看系统内核版本
9. 退出脚本程序
0. 显示帮助菜单
###########################
EOF
#2.提示用户输入要查看的信息
read -p "请根据上方菜单进行选择要查看的信息:" Num
#3. 根据用户输入的数字进行编写case语句
case $Num in
    1)
        echo "系统磁盘使用情况如下:"
        df -h
        ;;
    2)
        echo "系统内存使用情况如下:"
        free -m
        ;;
    3)
        echo "系统负载情况如下:"
        uptime
        ;;
    4)
        echo "系统CPU使用情况如下:"
        htop
        ;;
    5)
        echo "系统的Eth0网卡IP地址如下:"
        ifconfig eth0 | awk 'NR==2{print $2}'
        ;;
    6)
        echo "系统的外网IP地址如下:"
        curl -s ifconfig.me  
        echo
        ;;
    7)
        echo "操作系统版本如下:"
        awk '{print $1,$4}'  /etc/redhat-release
        ;;
    8)
        echo "系统内核版本如下:"
        uname -r
        ;;
    9)
        echo "脚本程序退出!"
        exit
        ;;
    0)
        echo "菜单!"
        ;;
    *)
        echo "你输入的不正确,请根据上方菜单进行选择!"
        exit
esac


while true
do
done

#循环格式
[root@shell /scripts/shell07]# cat case-1.sh
#!/bin/bash
#1. 编写菜单函数
menu() {
cat<<EOF
###########################
1. 显示磁盘的使用情况
2. 显示内存使用情况
3. 显示系统负载情况
4. 显示CPU使用情况
5. 查看系统Eth0网卡
6. 查看系统外网IP
7. 查看系统版本
8. 查看系统内核版本
9. 退出脚本程序
0. 显示帮助菜单
###########################
EOF
}
menu
#进行无限循环
while true
do
#2.提示用户输入要查看的信息
read -p "请根据上方菜单进行选择要查看的信息:" Num
#3. 根据用户输入的数字进行编写case语句
case $Num in
    1)
        echo "系统磁盘使用情况如下:"
        df -h
        ;;
    2)
        echo "系统内存使用情况如下:"
        free -m
        ;;
    3)
        echo "系统负载情况如下:"
        uptime
        ;;
    4)
        echo "系统CPU使用情况如下:"
        htop
        ;;
    5)
        echo "系统的Eth0网卡IP地址如下:"
        ifconfig eth0 | awk 'NR==2{print $2}'
        ;;
    6)
        echo "系统的外网IP地址如下:"
        curl -s ifconfig.me  
        echo
        ;;
    7)
        echo "操作系统版本如下:"
        awk '{print $1,$4}'  /etc/redhat-release
        ;;
    8)
        echo "系统内核版本如下:"
        uname -r
        ;;
    9)
        echo "脚本程序退出!"
        exit
        ;;
    0)
        menu
        ;;
    *)
        echo "你输入的不正确,请根据上方菜单进行选择!"
esac
done

12. 实现简单的Jumpserver

1. 执行脚本后,显示菜单,菜单内容所有可管理的主机

2. 选择菜单一个选项,连接对应的主机

3. 需要进行死循环,连接成功一个主机之后,退出还能继续连接其他的主机

4. 不能让其使用Ctrl c  d z 等操作  

5. 退出当前脚本的会话,再次登录时,还是自动的执行该脚本,不能操作后台的操作  /etc/bashrc  /etc/profile

6. 留一个出口,不让别人知道

菜单
#######################
1. 管理Web01-10.0.0.7
2. 管理Lb01-10.0.0.5
3. 管理Db01-10.0.0.51
4. 显示主机列表
#######################


[root@shell /scripts/shell07]# cat jumpserver.sh 
#!/bin/bash
#1.定义函数菜单
menu() {
cat<<EOF
#######################
1. 管理Web01-10.0.0.7
2. 管理Lb01-10.0.0.5
3. 管理Db01-10.0.0.51
4. 显示主机列表
#######################
EOF
}
#3.禁止输入Ctrl+c、d、z等操作
trap "" HUP INT TSTP  
#4.进行循环操作
while true
do
    menu
    #5. 提示用户输入对应的编号
    read -p "请根据上方菜单的主机列表,进行选择要连接的主机[1|2|3|4]:" Num
    #6.根据选择进行编写case语句
    case $Num in
        1)
            clear
            #7.测试对应的主机IP是否能通
            ping -c1 -W1 10.0.0.7 &>/dev/null
            if [ $? -eq 0 ];then
                echo "正在连接Web01-10.0.0.7主机........."
                ssh  root@10.0.0.7
            else
                echo "Web01的主机网络不可达!无法进行远程连接!"
            fi
            ;;
        2)
            clear
            #7.测试对应的主机IP是否能通
            ping -c1 -W1 10.0.0.5 &>/dev/null
            if [ $? -eq 0 ];then
                echo "正在连接Lb01-10.0.0.5主机........."
                ssh  root@10.0.0.5
            else
                echo "Lb01的主机网络不可达!无法进行远程连接!"
            fi
            ;;
        3)
            clear
            #7.测试对应的主机IP是否能通
            ping -c1 -W1 10.0.0.51 &>/dev/null
            if [ $? -eq 0 ];then
                echo "正在连接Db01-10.0.0.51主机........."
                ssh  root@10.0.0.51
            else
                echo "Lb01的主机网络不可达!无法进行远程连接!"
            fi
            ;;
        4)
            clear  #清屏
            ;;
        anhao)
            echo "内部人员才能执行的操作!退出程序!进入后台管理!"
            exit
            ;;
        *)
            echo "你输入的不符合要求!请根据菜单进行选择要连接的主机!"
            clear    
    esac
done

13. 实现一个多级菜单

1. 主菜单,两个辅菜单

安装Nginx和PHP

#########主菜单############
1. 安装Nginx服务
2. 安装PHP服务
3. 退出脚本
##########################

子菜单
########安装Nginx###########
1. 安装Nginx-1.6版本
2. 安装Nginx-1.7版本
3. 返回上一级菜单
###########################

子菜单
#########安装PHP############
1. 安装PHP-5.6版本
2. 安装PHP-7.1版本
3. 返回上一级菜单
###########################



[root@shell /scripts/shell07]# cat case-2.sh
#!/bin/bash
#1.定义菜单
menu() {
cat<<EOF
#########主菜单############
1. 安装Nginx服务
2. 安装PHP服务
3. 退出脚本
##########################
EOF
}
Nginx_menu() {
cat<<EOF
########安装Nginx###########
1. 安装Nginx-1.16版本
2. 安装Nginx-1.17版本
3. 返回上一级菜单
############################
EOF
}
Php_menu() {
cat<<EOF
#########安装PHP############
1. 安装PHP-5.6版本
2. 安装PHP-7.1版本
3. 返回上一级菜单
############################
EOF
}
#2.进入循环
while true
do
    #3.显示主菜单,提示用户根据菜单进行选择安装不同的服务
    menu
    read -p "请根据主菜单进行选择安装不同的服务:" Install
    case $Install in
        1)
            clear
            echo "你选择安装Nginx服务.............."
            while true
            do
                Nginx_menu
                read -p "请根据上方Nginx菜单进行选择安装不同的Nginx版本:" Nginx_Install
                case $Nginx_Install in
                    1)
                        clear
                        echo "你选择了安装Nginx-1.16版本..............."
                        echo "正在安装Nginx-1.16版本..................."
                        sleep 2
                        echo "Nginx-1.16版本安装成功..................."
                        ;;
                    2)
                        clear
                        echo "你选择了安装Nginx-1.17版本..............."
                        echo "正在安装Nginx-1.17版本..................."
                        sleep 2
                        echo "Nginx-1.17版本安装成功..................."
                        ;;
                    3)
                        clear
                        break #跳出当前循环,继续执行循环外面的命令
                        ;;
                    *)
                        clear
                        echo "你输入的不符合要求!"
                esac
            done
            ;;
        2)
            clear
            echo "你选择安装PHP服务.................."
            while true
            do
                Php_menu
                read -p "请根据上方PHP菜单进行选择安装不同的PHP版本:" PHP_Install
                case $PHP_Install in
                    1)
                        clear
                        echo "你选择了安装PHP-5.6版本.................."
                        echo "正在安装PHP-5.6版本......................"
                        sleep 2
                        echo "PHP-5.6版本安装成功......................"
                        ;;
                    2)
                        clear
                        echo "你选择了安装PHP-7.1版本.................."
                        echo "正在安装PHP-7.1版本......................"
                        sleep 2
                        echo "PHP-7.1版本安装成功......................"
                        ;;
                    3)
                        clear
                        break
                        ;;
                    *)
                        clear
                        echo "你输入的不符合要求!"
                esac
            done
            ;;
        3)
            clear
            echo "你选择了退出脚本!"
            exit
            ;;
        *)
            clear
            echo "你输入的不符合要求!"
    esac
done
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:44

results matching ""

    No results matching ""