案例1:编辑一个shell脚本,通过人机对话的方式修改IP地址
#!/bin/bash
 read -p "输入网卡接口名称:" ens
 read -p "输入IP地址:" ip
 read -p "输入子网掩码:" mask
 read -p "输入网关地址:" route
 read -p "输入首选DNS地址:" dnsifc="/etc/sysconfig/network-scripts/ifcfg-"
echo "DEVICE=$ens" > $ifc$ens
 echo "TYPE=Ethernet" >> $ifc$ens
 echo "BOOTPROTO=static" >> $ifc$ens
 echo "ONBOOT=yes" >> $ifc$ens
 echo "IPADDR=$ip" >> $ifc$ens
 echo "NETMASK=$mask" >> $ifc$ens
 echo "GATEWAY=$route" >> $ifc$ens    
 echo "DNS1=$dns" >> $ifc$enssystemctl restart network
 案例2:显示用户任意输入的两个数值,加减乘除后的结果与相除后的余数。
#!/bin/bash
 read -p "请输入数值1:"  num1
 read -p "请输入数值2:" num2
 a="$num1 + $num2 = `expr $num1 + $num2`"
 b="$num1 - $num2 = `expr $num1 - $num2`"
 c="$num1 \* $num2 = `expr $num1 \* $num2`"
 d="$num1 / $num2 = `expr $num1 / $num2`"
 e="$num1 % $num2 = `expr $num1 % $num2`"echo $a
 echo $b
 echo $c
 echo $d
 echo $e 案例3:通过设置变量,自动备份mysql的数据库
1.数据库的准备工作
创建数据库,创建表,添加数据,授权
[root@localhost /]# mysql -u root -p123.com
mysql> create database abc;
mysql> use abc;
mysql> create table biao1 (id int,name char(20));
 mysql> insert into biao1 values (1,'a1');
 mysql> insert into biao1 values (2,'a2');
 mysql> insert into biao1 values (3,'a3');mysql> create database guigu;
 mysql> use guigu;
 mysql> create table biao2 (id int,name char(20));
 mysql> insert into biao2 values (1,'b1');
 mysql> insert into biao2 values (2,'b2');
 mysql> grant all on *.* to 'zhang3'@'localhost' identified by '123.com';
 mysql> exit 2.创建备份目录
mkdir  /bak
12
 3.编辑备份脚本vim  /mysqlbak.sh
 添加:
 #!/bin/bash#添加各种变量
my_user=zhang3
 my_pass=123.com
 my_host=localhost
 my_conn="-u $my_user -p$my_pass -h $my_host"
 bak_command="mysqldump $my_conn"
 db_name1=abc
 db_name2=guigu
 bf_dir="/bak"
 bf_time=`date +%Y%m%d%H%M`
 bf_filename1="$db_name1-$bf_time.sql"
 bf_filename2="$db_name2-$bf_time.sql" #设置备份命令,调用变量
$bak_command $db_name1 > $bf_dir/$bf_filename1
 $bak_command $db_name2 > $bf_dir/$bf_filename24.添加计划任务
crontab -e
 添加:
 */2 * * * * sh /mysqlbak.sh案例4:判断用户输入的字符串是否为yes,如果是,则创建/data目录,并在/data目录中创建10个cctv开头的文件。
#!/bin/bash
 read -p "是否确认创建文件目录?(yes/no):  "  pif [ $p = "yes" ] ;then
     mkdir /data
     touch /data/cctv{1..10}.txt
     ls /data
 fi 案例5:判断两次输入的密码是否相同
#!/bin/bash
 read -p "请输入用户名:" user1
 stty -echo
 read -p "请输入密码:" pass1
 printf "\n"
 read -p "请再次输入密码:" pass2
 echo
 stty echoa=`cat /etc/passwd | grep $user1`
 if [ -z $a ] ;then
     useradd $user1
 fiif [ $pass1 = $pass2 ];then
     echo "正在给用户设置密码"
     echo "$pass1" | passwd $user1 --stdin &> /dev/null
     echo "密码设置完成"
 else
     echo "两次输入的密码不同,请重新输入"
 fi 案例6:用户输入一个成绩,判断是否为优秀,良好,合格,及格,不及格
#!/bin/bash
 read -p "请输入成绩:" num
 if [ $num -ge 90 ] ;then
     echo "优秀"
 elif [ $num -ge 80 ] && [ $num -lt 90 ] ;then
     echo "良好"
 elif [ $num -ge 70 ] && [ $num -lt 80 ] ;then
     echo "合格"
 elif [ $num -ge 60 ] && [ $num -lt 70 ] ;then
     echo "及格"
 elif [ $num -lt 60 ] ;then
     echo "凉凉"
 fi案例6的第二种语句写法:
 #!/bin/bash
 read -p "请输入成绩:" num
 if [ $num -ge 90 ] ;then
     echo "优秀"
 elif [ $num -ge 80 ];then
     echo "良好"
 elif [ $num -ge 70 ];then
     echo "合格"
 elif [ $num -ge 60 ];then
     echo "及格"
 else
     echo "凉凉"
 fi案例7:创建一个脚本,判断指定的服务是否启动?
#!/bin/bash
systemctl status $1 &> /dev/null
 if [ $? -eq 0 ];then
     echo "$1 服务已启动"
 else 
     echo "$1 服务未启动"
 fi案例8:创建一个脚本,判断指定的服务是否启动?如果未启动,则提示用户启动服务。
#!/bin/bash
systemctl status $1 &> /dev/null
 if [ $? -eq 0 ];then
     echo "$1 服务已启动"
 else 
     echo "$1 服务未启动"
     read -p "是否启动$1 服务?(y/n) "  a
     if [ $a = "y" ];then
         systemctl start $1  &> /dev/null
         if [ $? -eq 0 ];then
             echo "$1 服务已启动"
         else
             echo "$1 服务无法启动,可能是未安装或缺少配置"
         fi
     else
         echo "不启动服务,已退出程序"
     fifi
案例9:通过脚本,循环创建zhang1 zhang2  zhang3 zhang4 用户,并设置密码为123.com
#!/bin/bash
 for i in zhang1 zhang2 zhang3 zhang4
 do
     useradd $i
     echo "123.com" | passwd $i --stdin
 done 案例10:创建user.txt文件中的所有用户,并设置密码为123.com
vim /user.txt
 添加:
 zhang5
 zhang6
 zhang7
 zhang8
 zhang9
 zhang10
 zhang11
 zhang12
 zhang13
 zhang14
 zhang15
 zhang16#!/bin/bash
 a=`cat /user.txt`
 for i in $a
 do
         useradd $i
         echo "123.com" | passwd $i --stdin
 done 案例11:通过列表文件,测试目标主机是否可以通讯。
配置环境:给多台主机设置IP地址
 ifconfig ens33:1 192.168.10.2 netmask 255.255.255.0vim  /hostip.txt
 添加:
 192.168.10.2
 ......#!/bin/bash
 a=`cat /hostip.txt`
 for i in $a
 do
         ping $i -c 2 -i 0.01 -W 1 &> /dev/null
         if [ $? -eq 0 ];then
                 echo "主机$i 可以通讯。"
     else
         echo "主机$i 不能够通讯。"
     fi
 done案例12:显示指定位置中的文件和目录。
#!/bin/bash
a=`ls $1`
 for i in $a
 do
     if [ -d $1/$i ];then
         echo "$1/$i 是一个目录"
     elif [ -f $1/$i ];then
         echo "$1/$i 是一个文件"
     fidone
 案例13:判断当前登录的用户名与登录shell是否合法
#!/bin/bash
 a=`who | awk '{print $1}'`
 for i in $a
 do
     b=`cat /etc/passwd | grep "^$i" | awk -F: '{print $7}'`
     if [ $b = "/bin/bash" ];then
         echo "$i 是一个合法用户"
     else
         echo "$i 是非法用户,请清除该用户的登录。"
     fi
 done 案例14:创建liu1到liu20的连续数值的用户,设置默认密码为123.com
#!/bin/bash
 for i in {1..20}
 do
     useradd liu$i
     echo "123.com" | passwd liu$i --stdin
     或
     passwd    --stdin liu$i > 1.txt
 done案例15:计算1加到100的和
语法一:
#!/bin/bash
 a=0
 for i in {1..100}
 do
     a=`expr $a + $i`
 done
 echo $a 语法二:
#!/bin/bash
 a=0
 for i in `seq 1 100`    #seq  创建数组  起始数值  结束数值  ,生成数组的速度比正则表达式慢。
 do
     a=`expr $a + $i`
 done
 echo $a 案例16:计算1到100之间所有奇数的和
#!/bin/bash
 a=0
 for i in {1..100}
 do
     if [ `expr $i % 2` -eq 1 ];then
         a=`expr $a + $i`
     fi
     
 done
 echo $a 案例17:计算1到100之间所有偶数的和
#!/bin/bash
 a=0
 for i in {1..100}
 do
     if [ `expr $i % 2` -eq 0 ];then
         a=`expr $a + $i`
     fi
     
 done
 echo $a 案例18:使用for循环生成一个99乘法表   
方法一:
 #!/bin/bash
 for ((i=1;i<=9;i++))
 do
         for ((j=1;j<=i;j++))
         do
                 echo -n "$j*$i="`expr $i \* $j`
                 if [ `expr $i \* $j` -lt 10 ];then
                         echo -n "  "
                 else
                         echo -n " "
                 fi
         done
 echo
 done 方法二:
#!/bin/bash
 for ((i=1;i<=9;i++))
 do
         for ((j=1;j<=i;j++))
         do
                 echo -n "$j*$i="$((i*j))
                 if [ $((i*j)) -lt 10 ];then
                         echo -n "  "
                 else
                         echo -n " "
                 fi
         done
 echo
 done 方法三:
#!/bin/bash
 for i in {1..9}
 do
     for j in {1..9}
     do
         if [ $j -le $i ];then
              echo -n "$j*$i="$((i*j))
                     if [ $((i*j)) -lt 10 ];then
                                 echo -n "  "
                     else
                             echo -n " "
                     fi
         fi
     done
 echo
 done 案例19:创建指定的用户,并设置默认密码为123.com
#!/bin/bash
 for i in $*
 do
     useradd $i
     echo "123.com" | passwd $i --stdin
 done案例20:在当前目录下创建指定的子目录,权限默认分配777
#!/bin/bash
 for i in $*
 do
     mkdir $i
     chmod 777 $i
 done 案例21:使用while循环语句生成99乘法表
#!/bin/bash
a=1
 while [ $a -le 9 ]
 do
     b=1
     while [ $b -le $a ]
     do
         echo -n "$b*$a="$((a*b))
         if [ $((a*b)) -lt 10 ];then
             echo -n "  "
         else
             echo -n " "
         fi
         let b++
     done
 echo
 let a++
 done案例22:使用while语句计算1加到100的和
#!/bin/bash
 a=1
 b=0
 while [ $a -le 100 ]
 do
     b=$((b+a))
     let a++
 done
 echo "$b" 案例23:使用while语句计算1到100的奇数和
案例24:使用while语句计算1到100的偶数和
案例25:使用while语句绘制以下图形
      *
      ***
     *****
    *******
   *********
  ***********
 ************* #!/bin/bash
 a=1                # 定义行数起始变量
 b=1                # 定义*号的起始变量
 c=6                # 定义空格的起始变量while [ $a -le 7 ]        # 对行数进行判断循环,绘制指定的行数
 do
     c1=1            # 定义添加空格的变量
     while [ $c1 -le $c ]    # 添加空格的循环语句
     do
         echo -n " "
         let c1++
     done    b1=1            # 定义添加*号的变量
     while [ $b1 -le $b ]    # 添加*号的循环语句
     do
         echo -n "*"
         let b1++
     done
 echo                # 换行
 let a++                # 行数递增
 let b=$((b+2))            # *号数量递增2
 let c--                # 空格数量递减
 done案例:绘制下列图形
*
 **
 ***
 ****
 *****
 ****** #!/bin/bash
 a=1            # 初始行数变量
 b=1            # 每行*号总数变量while [ $a -le 6 ]    # 循环行数
 do
     b1=1        # 绘制*号初始变量
     while [ $b1 -le $b ]    # 循环绘制*号
     do 
         echo -n "*"
         let b1++
     done
 echo
 let a++
 let b++
 done案例27:绘制用户指定行数的*号
#!/bin/bash
 a=1
 b=1
 read -p "输入行数:" n
 while [ $a -le $n ]
 do
     b1=1
     while [ $b1 -le $b ]
     do 
         echo -n "*"
         let b1++
     done
 echo
 let a++
 let b++
 done案例28:绘制下列图形
*
 **
 ***
 ****
 *****
 ******
 *****
 ****
 ***
 **
 *#!/bin/bash
 a=1
 b=1
 while [ $a -le 6 ]
 do
     b1=1
     while [ $b1 -le $b ]
     do 
         echo -n "*"
         let b1++
     done
 echo
 let a++
 let b++
 donec=$((a-2))        # 指定变量中的数据来自于上一组循环中变量的最后赋值
 d=$((b-2))        # 
 while [ $c -ge 1 ]
 do
     d1=1
     while [ $d1 -le $d ]
     do
         echo -n "*"
         let d1++
     done
 echo
 let c--
 let d--
 done 作业练习1:
      *
      ***
     *****
    *******
   *********
  ***********
   *********
    *******
     *****
      ***
       *作业练习2:
       ***  ***
      **********
     ************
      **********
       ********
        ******
         ****
          **案例29:猜猜看,程序随机生成一个物品的价格,用户可以猜测,猜出后,显示猜测次数。
#!/bin/bash
 count=0                # 计数器变量
 b=$(expr $RANDOM % 1000)    # 生成一个1000以内的随机整数
 while true
 do
     let count++
     read -p "请输入商品的价格:" num
     if [ $num -eq $b ];then
         echo "您猜对了,价格是$num"
         echo "您总共猜了 $count 次"
         exit 0
     elif [ $num -lt $b ];then
         echo "您猜测的价格低了"
     else
         echo "您猜测的价格高了"
     fidone
案例30:使用while语句,编辑一个密码验证的脚本,如果有空密码或密码不同时,要求重新输入。
编程思路:
 1.先编写脚本的主框架,完成基本功能。
 2.完整脚本中的其他需求。
 3.完善循环语句机构。
 4.执行脚本,找到不足之处,继续修改。
 5.完善脚本后,添加新的功能。(根据环境或需求)#!/bin/bash
 while true
 do
 read -p "请输入用户名:" uu
 stty -echo
 read -p "请输入密码:" pass1
 echo
 read -p "请再次输入密码:" pass2
 echo
 stty echoif [ -z $pass1 ] || [ -z $pass2 ] ;then
     echo "输入的密码不能为空,请重新输入"elif [ $pass1 = $pass2 ];then
     echo "正在创建 $uu 用户"
     useradd $uu
     echo $pass1 | passwd $uu --stdin &> /dev/null
     echo "用户 $uu 创建完成"
     exit 0
 else
     echo "两次输入的密码不同,请重新输入"
 fi
 done 案例31:输入用户名密码后,与数据库中的用户名密码进行对比验证。
1.建立数据库,在数据库中添加用户名和密码。
安装数据库:
 yum -y install mariadb mariadb-server mariadb-devel启动数据库服务:
 systemctl restart mariadb
初始化root密码:
 mysqladmin -u root password 123.com
登录数据库:
mysql -u root -p123.com
创建数据库:
MariaDB [(none)]> create database abc;
创建表:
 MariaDB [(none)]> use abc;
 MariaDB [abc]> create table login (id int,name char(20),pass char(20));添加数据:
MariaDB [abc]> insert into login values (1,'zhang1','123.com');
 MariaDB [abc]> insert into login values (2,'zhang2','123456');
 MariaDB [abc]> insert into login values (3,'zhang3','123');
 MariaDB [abc]> exit 2.创建shell脚本
编程思路:
 1.先编写脚本的主框架,完成基本功能。
 2.完整脚本中的其他需求。
 3.完善循环语句机构。
 4.执行脚本,找到不足之处,继续修改。
 5.完善脚本后,添加新的功能。(根据环境或需求) #!/bin/bash
while true
 do
 read -p "请输入登录用户名称: " uu
 read -p "请输入登录密码:" pass1a=$(mysql -u root -p123.com -e "select pass from abc.login where name='$uu'")
 if [ -z $pass1 ];then
     echo "密码不能为空,请重新输入"
 elif [ -z $a ];then
     echo "该用户不存在"
 else
     a1=$(echo $a | awk '{print $2}')
     if [ $pass1 = $a1 ];then
         echo "用户已登录"
         exit 0
     else
         echo "用户名或密码错误,请重新登录"
     fi
 fidone
 应用案例32:在/book目录下有很多html的页面文件,如小说页面文件,文件名起名不合理,导致排序错误。希望通过一个shell脚本,将所有的文件名进行统一的改名并排序。
 原有文件名a1.html到a500.html,修改为a001.html到a500.html准备工作:
 mkdir /book
 cd /book
 touch a{1..500}.html#!/bin/bash
 a=`ls /book`
 for i in $a
 do
     c=$(echo $i |awk -F. '{print $1}' | awk -Fa '{print $2}')
         if [ $c -le 9 ];then
             mv /book/$i /book/a00$c.html
         elif [ $c -le 99 ];then
             mv /book/$i /book/a0$c.html
             
         fi
     
 done 应用案例33:创建一个脚本,能够对Mysql数据库进行添加数据,查询数据的操作。
准备工作:
 创建数据库abc,创建表。 #!/bin/bash
while true
 do
     echo "            A   添加数据 "
     echo "            B   查询数据 "
     echo "            Q   退出程序 "
 echo
 echo
 read -p "请输入操作序号:" numclear -n        #清除屏幕上的信息
 if [ -z $num ];then
     echo "选项不能为空,请重新选择!"elif [ $num = "A" ];then
     while true
     do
         read -p "输入name字段的值:" name1
         if [ -z $name1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入sex字段的值:" sex1
         if [ -z $sex1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入age字段的值:" age1
         if [ -z $age1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入zw字段的值:" zw1
         if [ -z $zw1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
         
         mysql -u root -p123.com -e "insert into abc.info (name,sex,age,zw) values ('$name1','$sex1',$age1,'$zw1')"
         
         
     
 elif [ $num = "B" ];then    read -p "请输入查询姓名字段的关键字:" name1
     if [ -z $name1 ];then
         mysql -u root -p123.com -e "select * from abc.info"
     else
     mysql -u root -p123.com -e "select * from abc.info where name='$name1'"
     echo    #换行
     echo
     echo
     fi
 elif [ $num = "Q" ];then
     exit 0
 else
     echo "您输入的选项不正确,请重新输入!"
 fidone
案例34:创建一个管理NFS服务的脚本
./nfs  start 
#!/bin/bash
 case $1 instart)
     systemctl start nfs
     ;;
 stop)
     systemctl stop nfs
     ;;
 status)
     systemctl ststus nfs &> /dev/null
     if [ $? -eq 0 ];then
         echo "NFS 服务已启动"
     else
         echo "NFS 服务未启动"
     fi
     ;;
 restart)
     $0 stop        #$0表示命令本身,具体调用格式为 nfs stop,在脚本中使用这种语法时,脚本必须设置为可执行权限。也可以用 sh $0  stop这种格式(此格式不实用)。
     $0 start
     ;;
 *)
     echo "请使用 start | stop | status | restart 等选项"
     ;;
 esac 应用案例35:使用循环+case语句实现对Mysql数据库进行添加数据,查询数据的操作。
 #!/bin/bash
while true
 do
     echo "            A   添加数据 "
     echo "            B   查询数据 "
     echo "            Q   退出程序 "
 echo
 echo
 read -p "请输入操作序号:" numclear -n        #清除屏幕上的信息
 if [ -z $num ];then
     echo "选项不能为空,请重新选择!"
 elsecase $num in
A|a)
     while true
     do
         read -p "输入name字段的值:" name1
         if [ -z $name1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入sex字段的值:" sex1
         if [ -z $sex1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入age字段的值:" age1
         if [ -z $age1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
     while true
     do
         read -p "输入zw字段的值:" zw1
         if [ -z $zw1 ];then
             echo "数值不能为空,请重新输入"
         else
             break
         fi
     done
         
         mysql -u root -p123.com -e "insert into abc.info (name,sex,age,zw) values ('$name1','$sex1',$age1,'$zw1')"
     ;;
 B|b)
     read -p "请输入查询条件表达式:" aa
     if [ -z $aa ];then
         mysql -u root -p123.com -e "select * from abc.info"
     else
         mysql -u root -p123.com -e "select * from abc.info where  $aa"    fi
     ;;Q|q)
     exit 0
     ;;
 *)
     echo "您输入的选择不存在,请重新输入!!"
 esac
 fi
 done应用案例36:使用shell,建立一个游戏人物的创建环境,创建人物时的职业选择,随机生成的能力点数,将生成的数据写入数据库。
 结构分析:
 职业--战士   骑士    法师
 能力--力量   体力   精神   智力数据匹配范围:
 战士:力量最高,体力次之,精神与智力最少
 骑士:体力最高,力量与精神次之,智力最少
 法师:精神与智力最高,力量与体力最少数据库的建立
create  database games;
create table info (id int primary key auto_increment,name char(20),zy char(20),li int,ti int,jin int,zhi int);
 #!/bin/bash
 while true
 do
     echo "A  创建角色"
     echo "B  查看角色"
     echo "D  删除角色"
     echo "Q  退出"
 echo
 echo
     read -p "请输入你的操作:" num
 clear  -n
 if [ -z $num ];then
     echo "不能为空,请输入您的操作!!"
 else
 case $num inA|a)
     read -p "请输入角色名称:"  name1
     echo "1 战士"
     echo "2 骑士"
     echo "3 法师"
     read -p "请选择角色职业:" num1    case $num1 in
     
     1)
         while true
         do
         l=$(expr $RANDOM % 100)
         l=$((l*10))
         t=$(expr $RANDOM % 100)
         t=$((t*5))
         j=$(expr $RANDOM % 100)
         z=$(expr $RANDOM % 100)
         echo "力量:$l"
         echo "体力:$t"
         echo "精神:$j"
         echo "智力:$z"
         echo
         read -p "是否确实以上信息(y|n)" pp
         if [ $pp = y ];then
         mysql -u root -p123.com -e "insert into games.info (name,zy,li,ti,jin,zhi) values ('$name1','$num1',$l,$t,$j,$z)"
         break
         fi
         done
         ;;    2)
         while true
         do
         l=$(expr $RANDOM % 100)
         l=$((l*5))
         t=$(expr $RANDOM % 100)
         t=$((t*10))
         j=$(expr $RANDOM % 100)
         j=$((j*5))
         z=$(expr $RANDOM % 100)
         echo "力量:$l"
         echo "体力:$t"
         echo "精神:$j"
         echo "智力:$z"
         echo
         read -p "是否确实以上信息(y|n)" pp
         if [ $pp = y ];then
         mysql -u root -p123.com -e "insert into games.info (name,zy,li,ti,jin,zhi) values ('$name1','$num1',$l,$t,$j,$z)"
         break
         fi
         done
         ;;    3)    
         while true
         do
         l=$(expr $RANDOM % 100)
         t=$(expr $RANDOM % 100)
         j=$(expr $RANDOM % 100)
         j=$((j*10))
         z=$(expr $RANDOM % 100)
         z=$((z*10))
         echo "力量:$l"
         echo "体力:$t"
         echo "精神:$j"
         echo "智力:$z"
         echo
         read -p "是否确实以上信息(y|n)" pp
         if [ $pp = y ];then
         mysql -u root -p123.com -e "insert into games.info (name,zy,li,ti,jin,zhi) values ('$name1','$num1',$l,$t,$j,$z)"
         break
         fi
         done        ;;
    esac
     ;;
 B|b)
     
     read -p "请输入角色名称:" name2
     b1=$(mysql -u root -p123.com -e "select * from games.info where name=$name2" |grep $name2 | awk '{print $3}')
     b2=$(mysql -u root -p123.com -e "select * from games.info where name=$name2" |grep $name2 | awk '{print $4}')
     b3=$(mysql -u root -p123.com -e "select * from games.info where name=$name2" |grep $name2 | awk '{print $5}')
     b4=$(mysql -u root -p123.com -e "select * from games.info where name=$name2" |grep $name2 | awk '{print $6}')
     b5=$(mysql -u root -p123.com -e "select * from games.info where name=$name2" |grep $name2 | awk '{print $7}')    case $b1 in
     1)
         echo "名称:$name2"
         echo "职业:战士"
         echo "力量:$b2"
         echo "体力:$b3"
         echo "精神:$b4"
         echo "指令:$b5"
         ;;
     2)
         echo "名称:$name2"
         echo "职业:骑士"
         echo "力量:$b2"
         echo "体力:$b3"
         echo "精神:$b4"
         echo "指令:$b5"
         ;;
     3)
         echo "名称:$name2"
         echo "职业:法师"
         echo "力量:$b2"
         echo "体力:$b3"
         echo "精神:$b4"
         echo "指令:$b5"
         ;;
     esac
     ;;
 D|d)
     read -p "请输入要删除的角色名称:" name3
     mysql -u root -p123.com -e "delete from games.info where name=$name3"
     ;;Q|q)
     exit 0
     ;;esac
 fi
 done 案例37:编写脚本,自动配置named服务,自动添加www.guiguedu.cn域名
 #!/bin/bash
sed -i "s/127\.0\.0\.1/192\.168\.10\.2/g"  /etc/named.conf
 sed -i "11,21s/localhost/any/g" /etc/named.confecho 'zone "guiguedu.cn" IN {' >> /etc/named.rfc1912.zones
 echo '        type master; ' >> /etc/named.rfc1912.zones
 echo '        file "guiguedu.cn.zone"; ' >> /etc/named.rfc1912.zones
 echo '        allow-update { none; }; ' >> /etc/named.rfc1912.zones
 echo '};' >> /etc/named.rfc1912.zonescp -p /var/named/named.localhost /var/named/guiguedu.cn.zone
sed -i "s/@/www.guiguedu.cn./2" /var/named/guiguedu.cn.zone
 sed -i "3,8s/@/www.guiguedu.cn./g" /var/named/guiguedu.cn.zonesed -i "9,+5d" /var/named/guiguedu.cn.zone
 echo "www  IN  A  192.168.10.2" >> /var/named/guiguedu.cn.zone综合应用案例38:编辑一个脚本,自动添加用户指定域名与IP地址,不能出现重复的域名。
#!/bin/bash
 read -p "是否是第一次配置named服务?(y/n):"  yy
 if [ $yy = y ];then
 b=$(ifconfig ens33 | grep "inet " |awk '{print $2}')sed -i "s/127.0.0.1/$b/g"  /etc/named.conf
 sed -i "11,21s/localhost/any/g" /etc/named.conf
 fiwhile true
 do
 read -p "请输入要添加的域名:" uu
 read -p "请输入主机IP地址:" ip
 a=$(grep $uu /etc/named.rfc1912.zones)
 if [ -z "$a" ];thenecho "zone "'"'$uu'" ' "IN {" >> /etc/named.rfc1912.zones 
 echo '        type master; ' >> /etc/named.rfc1912.zones
 echo "        file" '"'$uu.zone'"'"; " >> /etc/named.rfc1912.zones
 echo '        allow-update { none; }; ' >> /etc/named.rfc1912.zones
 echo '};' >> /etc/named.rfc1912.zonescp -p /var/named/named.localhost /var/named/$uu.zone
sed -i "s/@/www.$uu./2" /var/named/$uu.zone
 sed -i "3,8s/@/www.$uu./g" /var/named/$uu.zonesed -i "9,+5d" /var/named/$uu.zone
 echo "www  IN  A  $ip" >> /var/named/$uu.zoneecho "$uu域名已注册完成,添加了默认的A记录www 地址为$ip"
 break
 else
     read -p "该域名$uu已经注册过,是否更换一个域名?按y则重新输入域名,按n则给此域名添加新的主机。(y/n):" yy1
     if [ $yy1 = n ];then
         read -p "请输入主机名:" n1
         read -p "请输入主机地址:" ip1
         echo "$n1  IN  A  $ip1" >> /var/named/$uu.zone
     fi
 fi
 done综合应用案例39:创建一个脚本,能够自动配置dhcp服务。
#!/bin/bash
a=$(ifconfig ens33 | grep "inet " |awk '{print $2}' | awk -F. '{print $1"."$2"."$3"."0}')
read -p "请输入地址池起始范围:" ip1
 read -p "请输入地址池结束范围:" ip2
 read -p "请输入网关地址:" ip3
 read -p "请输入首选DNS地址:" ip4echo "subnet $a netmask 255.255.255.0 {" >> /etc/dhcp/dhcpd.conf
 echo "range $ip1 $ip2;" >> /etc/dhcp/dhcpd.conf
 echo "option routers $ip3;" >> /etc/dhcp/dhcpd.conf
 echo "option domain-name-servers $ip4;" >> /etc/dhcp/dhcpd.conf
 echo "}" >> /etc/dhcp/dhcpd.conf综合应用案例40:创建一个脚本,能够自动配置samba服务脚本:
#!/bin/bash
read -p "请输入共享目录的路径:" p
 read -p "请输入共享名:" n
 if [ -e $p ];then
     if [ -d $p ];then
         echo "[$n]" >> /etc/samba/smb.conf
         echo "    path = $p" >> /etc/samba/smb.conf
         echo "    browseable = yes" >> /etc/samba/smb.conf
         echo "    read only = no" >> /etc/samba/smb.conf    else
         echo "$p 不是目录"
     fi
 else
     echo "$p 不存在"
 fi 综合应用案例41:编辑一个提供菜单选项的named服务管理脚本。
菜单样式:
 1 配置全局文件
 2 注册新域名
 3 添加主机
 4 删除主机
 5 删除域名
 0 退出 #!/bin/bash
 while true
 do
     echo "        1 配置全局文件"
     echo "        2 注册新域名"
     echo "        3 添加主机"
     echo "        4 删除主机"
     echo "        5 删除域名"
     echo "        0 退出"
 echo
 echoread -p "请选择执行代码:"  n
clear -n
case $n in
 1)
     
     b=$(ifconfig ens33 | grep "inet " |awk '{print $2}')    sed -i "s/127.0.0.1/$b/g"  /etc/named.conf
     sed -i "11,21s/localhost/any/g" /etc/named.conf
     ;;
 2)
     while true
     do
     read -p "请输入要添加的域名:" uu
     read -p "请输入主机IP地址:" ip
     a=$(grep $uu /etc/named.rfc1912.zones)
     if [ -z "$a" ];then    echo "zone "'"'$uu'" ' "IN {" >> /etc/named.rfc1912.zones 
     echo '        type master; ' >> /etc/named.rfc1912.zones
     echo "        file" '"'$uu.zone'"'"; " >> /etc/named.rfc1912.zones
     echo '        allow-update { none; }; ' >> /etc/named.rfc1912.zones
     echo '};' >> /etc/named.rfc1912.zones    cp -p /var/named/named.localhost /var/named/$uu.zone
    sed -i "s/@/www.$uu./2" /var/named/$uu.zone
     sed -i "3,8s/@/www.$uu./g" /var/named/$uu.zone    sed -i "9,+5d" /var/named/$uu.zone
     echo "www  IN  A  $ip" >> /var/named/$uu.zone    echo "$uu域名已注册完成,添加了默认的A记录www 地址为$ip"
     break
     else
         echo "无法添加$uu 域名。"
     break
     fi
     done
     ;;3)
     while true
     do
     read -p "请输入添加主机的域名,输入exit退出当前选项:" uu1
     a=$(grep $uu1 /etc/named.rfc1912.zones)
     
     if [ $uu1 = exit ];then
             break
     elif [ -z "$a" ];then
         echo "域名不存在,请重新输入域名!"
     
     else
         while true
         do
         read -p "请输入主机名:" n1
         b=$(grep $n1 /var/named/$uu1.zone |awk '{print $1}')
         if [ -z $b ];then
             read -p "请输入主机地址:" ip1
             echo "$n1  IN  A  $ip1" >> /var/named/$uu1.zone
             break
         else
             echo "主机名以存在,请重新输入"        fi
         done
     break
         
     fi
     done
     ;;
 4)    while true
     do
     read -p "请输入需要删除主机的域名,输入exit退出:" uu2
     c=$(grep $uu2 /etc/named.rfc1912.zones)
     if [ $uu2 = exit ];then
             break
     elif [ -z "$c" ];then
         echo "域名不存在,请重新输入域名!"
     else
         while true
         do
         read -p "请输入主机名:" n2
         d=$(grep $n2 /var/named/$uu2.zone |awk '{print $1}')
         if [ -z $d ];then
             echo "主机不存在,无法删除,请从新输入"
         else
             sed -i "/$n2/d" /var/named/$uu2.zone
             break
         fi
         done
     break
         
     fi
     done
     ;;5)
     while true
     do
     read -p "请输入需要删除的域名,输入exit退出::" uu2
     c=$(grep $uu2 /etc/named.rfc1912.zones)
     if [ $uu2 = exit ];then
             break
     elif [ -z "$c" ];then
         echo "域名不存在,请重新输入域名!"
     else
         read -p "请确认是否要删除$uu2域名(y/n):" yy
         if [ $yy = y ];then
             sed -i "/$uu2/,+5d" /etc/named.rfc1912.zones
             rm -f /var/named/$uu2.zone
             break
         else
             echo "放弃删除操作"
         fi
         break
     fi
     done
     ;;0)
     exit 0
     ;;
 esac
 done 综合应用案例42:编辑脚本,对新安装的centos系统进行初始化。
分析:需要对系统初始化的内容。
1.yum安装源的配置
 2.gcc  gcc-c++组件的安装
 3.kernel-devel运行环境的安装
 4.防火墙的关闭,selinux的关闭
 5.open-vm-tools的卸载
 6.vmware-tools的安装 准备工作:收集相关的软件包
gcc  gcc-c++  kernel-devel  相关依赖环境软件包
 编辑脚本:
#!/bin/bash
 echo "正在配置yum源...."
 mkdir /etc/yum.repos.d/bak
 mv /etc/yum.repos.d/* /etc/yum.repos.d/bakecho "[yum]" > /etc/yum.repos.d/yum.repo
 echo "baseurl=file:///media" >> /etc/yum.repos.d/yum.repo
 echo "enabled=1" >> /etc/yum.repos.d/yum.repo
 echo "gpgcheck=0" >> /etc/yum.repos.d/yum.repo
 echo 
 echo "yum源配置完成。"
 echo
 echo "正在安装gcc gcc-c++ kernel-devel环境......"yum gcc gcc-c++ kernel-devel &> /dev/null
#rpm -ivh libmpc-1.0.1-3.el7.x86_64.rpm
 #rpm -ivh cpp-4.8.5-11.el7.x86_64.rpm
 #rpm -ivh kernel-headers-3.10.0-514.el7.x86_64.rpm
 #rpm -ivh glibc-headers-2.17-157.el7.x86_64.rpm
 #rpm -ivh glibc-devel-2.17-157.el7.x86_64.rpm
 #rpm -ivh gcc-4.8.5-11.el7.x86_64.rpm
 #rpm -ivh libstdc++-devel-4.8.5-11.el7.x86_64.rpm
 #rpm -ivh gcc-c++-4.8.5-11.el7.x86_64.rpm
 #rpm -ivh kernel-devel-3.10.0-514.el7.x86_64.rpmecho "环境已经安装完成"
 echo
 echo "正在关闭防火墙和selinux安全选项...."
 systemctl stop firewalld.service
 systemctl disable firewalld
 setenforce 0
 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g"  /etc/sysconfig/selinux
 echo
 echo "防火墙已关闭"echo "正在安装vmware-tools工具...."
 rpm -e open-vm-tools-desktop --nodeps
 rpm -e open-vm-tools --nodepstar -zxf VMwareTools-10.0.0-2977863.tar.gz
 cd vmware-tools-distrib/
 ./vmware-install.pl -d -f &> /dev/nullecho "vmwar-tools已安装"
reboot
 软件打包:
tar -zcvf /setup.tar.gz /setup
案例43:编辑脚本,自动安装部署源代码LAMP环境。
分析:
 1.源代码安装LAMP环境的顺序。先安装apache,mysql,php。
 2.安装LAMP的依赖环境软件包。有RPM包,有源代码包。
 3.对软件的配置#!/bin/bash
 echo "安装apache服务"# 安装依赖环境
yum -y install apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util apr-util-devel pcre-devel pcre  &> /dev/null
  
 # 配置安装httpd服务tar -zxf httpd-2.4.25.tar.gz -C /usr/src
 cd /usr/src/httpd-2.4.25 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-sockets
make && make install
 # 优化路径与服务
ln -s /usr/local/httpd/bin/* /usr/local/bin
 cp /usr/local/httpd/bin/apachectl /etc/rc.d/init.d/httpdsed -i "1a #chkconfig: 35 85 21" /etc/rc.d/init.d/httpd 
 chmod a+x /etc/rc.d/init.d/httpd chkconfig --add httpd
 chkconfig httpd onecho  "安装mysql数据库"
 yum -y install ncurses-devel 
tar -zxvf cmake-2.8.6.tar.gz -C /usr/src
 tar -zxvf mysql-5.6.36.tar.gz -C /usr/srccd /usr/src/cmake-2.8.6/
./configure && gmake && gmake install
cd /usr/src/mysql-5.6.36/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
make  &&  make install
# 配置mysql
useradd  -M -s /sbin/nologin  mysql
chown -R mysql:mysql /usr/local/mysql/
rm -f /etc/my.cnf
 cp /usr/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnfcd /lamp
 yum -y install perl perl-devel perl-DBD*# 初始化mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 优化服务脚本
ln -s /usr/local/mysql/bin/* /usr/local/bin
cp /usr/src/mysql-5.6.36/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod a+x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
 echo  "安装php服务"
# 安装PHP相关依赖环境
cd /lamp
yum -y inststall xz-devel zlib-devel libxml2-devel
 # 安装PHP相关的加密算法与校验组件
 tar -zxvf libmcrypt-2.5.8.tar.gz -C /usr/src
 tar -zxvf mcrypt-2.6.8.tar.gz -C /usr/src
 tar -zxvf mhash-0.9.9.9.tar.gz -C /usr/srccd /usr/src/libmcrypt-2.5.8/
 ./configure && make && make install
 ln -s /usr/local/lib/libmcrypt.* /usr/lib/cd /usr/src/mhash-0.9.9.9/
 ./configure && make && make install
 ln -s /usr/local/lib/libmhash* /usr/lib/cd /usr/src/mcrypt-2.6.8
 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
 ./configure && make && make install # 配置安装PHP主程序
cd /lamp
 tar -zxvf php-5.5.38.tar.gz -C /usr/srccd /usr/src/php-5.5.38/
./configure --prefix=/usr/local/php --with-mcrypt --with-apxs2=/usr/local/httpd/bin/apxs --with-mysql=/usr/local/mysql  --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php --enable-mbstring --enable-sockets
 make  && make install
 echo "配置lamp"
 # 配置httpd.conf,添加php应用进程
 sed -i "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/g" /usr/local/httpd/conf/httpd.conf
sed -i "/#AddType application/a AddType application\/x-httpd-php .php" /usr/local/httpd/conf/httpd.conf
 systemctl restart httpd
 ststemctl restart mysqld