1. shell case语法讲解
1.1 意义
快速的了解操作
修改的准确性
提高工作的准确性
1.2 语法
case vars in
1) echo "在上课"
;;
2) echo "在学习"
;;
*)
exit 1
esac
1.3 实操
[root@backup scripts]# sh test_case.sh
请输入你现在要干什么2
在学习
[root@backup scripts]# vim test_case.sh
read -p "请输入你现在要干什么" vars
case $vars in
1) echo "在上课"
;;
2) echo "在学习"
;;
*) echo "please input ture num"
exit 1
esac
2 脚本展示
#!/bin/bash chmod 777 /etc/sysconfig/network-scripts/ifcfg-`route -n | awk 'END{print $NF}'` ##主网卡配置文件 Main_eth="/etc/sysconfig/network-scripts/ifcfg-`route -n | awk 'END{print $NF}'`" ##原先的IP地址 Original_ip=$(/sbin/ifconfig `/sbin/route -n | awk 'END{print $NF}'` | awk -F "[ :]+" 'NR==2{print $4}') ##原先的mac地址 Original_mask=$(/sbin/ifconfig `/sbin/route -n | awk 'END{print $NF}'` | awk -F "[ :]+" 'NR==2{print $NF}') ##原先网关地址 Original_gateway=$(/sbin/route -n | awk 'END{print $2}') ##要添加的DNS地址 Add_DNS1='DNS3=114.114.114.114' IP_TRUE(){ read -p "请输入你要替换的IP地址 :" Modify_ip ##判断IP是否符合规则 check_ip(){ #echo $Modify_ip | grep -E "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" > /dev/null; echo $Modify_ip|grep "^[0-9]{1,3}.([0-9]{1,3}.){2}[0-9]{1,3}$" > /dev/null if [ $? -ne 0 ] then echo "IP地址输入错误,必须全部是数字" return 1 fi ##将IP地址的每段用变量找出来 a=$(echo $Modify_ip|awk -F . '{print $1}') b=$(echo $Modify_ip|awk -F . '{print $2}') c=$(echo $Modify_ip|awk -F . '{print $3}') d=$(echo $Modify_ip|awk -F . '{print $4}') ##遍历IP地址每段的数字 for num in $a $b $c $d do if [ $num -gt 255 ] || [ $num -lt 0 ] then echo $Modify_ip"中,字段"$num"错误" return 1 fi done echo $Modify_ip "合法,可以替换成这个IP" return 0 } ##判断IP地址是不是空的 if [ ! -n "$Modify_ip" ] then echo "请输入IP,IP输入不符合常规写法" return 1 else check_ip sed -i "s#${Original_ip}#${Modify_ip}#g" ${Main_eth} fi } ##判断mac地址是否符合规则 MASK_TRUE(){ read -p "请输入你要替换的mac地址 :" Modify_mask check_mask(){ echo $Modify_mask | grep -E "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" > /dev/null; if [ $? -ne 0 ] then echo "mac地址输入错误,必须全部是数字" return 1 fi ##将mac地址的每段用变量找出来 a=$(echo $Modify_mask|awk -F . '{print $1}') b=$(echo $Modify_mask|awk -F . '{print $2}') c=$(echo $Modify_mask|awk -F . '{print $3}') d=$(echo $Modify_mask|awk -F . '{print $4}') ##遍历mac地址每段的数字 for num in $a $b $c $d do if [ $num -gt 255 ] || [ $num -lt 0 ] then echo $Modify_mask"中,字段"$num"错误" return 1 fi done echo $Modify_mask "合法,可以替换成这个IP" return 0 } ##判断mac地址是不是空的 if [ ! -n "$Modify_mask" ] then echo "请输入IP,IP输入不符合常规写法" return 1 else check_mask sed -i "s#${Original_mask}#${Modify_mask}#g" ${Main_eth} fi } GATEWAY_TRUE(){ read -p "请输入你要替换的网关地址 :" Modify_gateway check_gateway(){ echo $Modify_gateway | grep -E "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" > /dev/null; if [ $? -ne 0 ] then echo "网关地址输入错误,必须全部是数字" return 1 fi ##将mac地址的每段用变量找出来 a=$(echo $Modify_gateway|awk -F . '{print $1}') b=$(echo $Modify_gateway|awk -F . '{print $2}') c=$(echo $Modify_gateway|awk -F . '{print $3}') d=$(echo $Modify_gateway|awk -F . '{print $4}') ##遍历mac地址每段的数字 for num in $a $b $c $d do if [ $num -gt 255 ] || [ $num -lt 0 ] then echo $Modify_gateway"中,字段"$num"错误" return 1 fi done echo $Modify_gateway "合法,可以替换成这个IP" return 0 } ##判断IP地址是不是空的 if [ ! -n "$Modify_gateway" ] then echo "请输入IP,IP输入不符合常规写法" return 1 else check_gateway sed -i "s#${Original_gateway}#${Modify_gateway}#g" ${Main_eth} fi } ##主网卡怎么获取ip地址的方式 Main_bro=$(grep -iE "^BO*" $Main_eth) #Main_bro='abc' ##修改网卡的类型 Modify_dhcp='BOOTPROTO=dhcp' ##修改网卡的类型 Modify_static='BOOTPROTO=static' ##修改网卡的类型 Modify_none='BOOTPROTO=none' BRO_TRUE(){ if [ "$Main_bro" = "$Modify_none" ] then echo "现在IP连接方式是: "$Modify_none sed -i "s#${Modify_none}#${Modify_dhcp}#g" ${Main_eth} echo "修改后IP连接方式是: "$Modify_dhcp elif [ "$Main_bro" = "$Modify_static" ] then echo "现在IP连接方式是: "$Modify_static sed -i "s#${Modify_static}#${Modify_dhcp}#g" ${Main_eth} echo "修改后IP连接方式是: "$Modify_dhcp else echo "现在的IP连接方式是 "$Modify_dhcp sed -i "s#${Modify_dhcp}#${Modify_static}#g" ${Main_eth} echo "修改后IP连接方式是: "$Modify_static fi } ETH_TRUE(){ date=`date +%F-%T` read -p "请输入你要修改的网卡(例如ifcfg-eth0) " eth chmod 777 /etc/sysconfig/network-scripts/$eth PATH_ETH="/etc/sysconfig/network-scripts/$eth" DST_PATH="/etc/sysconfig/network-script/ifcfg_bakup_liangyuxing/" cp -rf /etc/sysconfig/network-scripts/$eth /etc/sysconfig/network-scripts/$eth_adam_$date cp -rf /etc/sysconfig/network-scripts/$eth_adam_$date $DST_PATH if [ $? -eq 0 ] then echo "备份成功" elif [ ! -d "$DST_PATH" ] then mkdir -p "$DST_PATH" fi DEFROUTE=$(grep -iE "DEFROUTE" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') PEERDNS=$(grep -iE "PEERDNS" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') PEERROUTES=$(grep -iE "PEERROUTES" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') sed -i "s#${DEFROUTE}#no#g" $PATH_ETH sed -i "s#${PEERDNS}#no#g" $PATH_ETH sed -i "s#${PEERROUTES}#no#g" $PATH_ETH read -p "请问你是要在原先的上面需要修改网卡连接类型吗,如果修改请输入"y"其他键则退出 " Bro_Enter if [ "$Bro_Enter" == y ] then read -p "请输入你现在的网卡连接类型(例如:static,none,dhcp) " ORI_BRO ORI_BRO=$(grep -iE "BOOTPROTO" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') read -p "请输入修改后的网卡类型 " MIN_BRO sed -i "s#${ORI_BRO}#${MIN_BRO}#g" ${PATH_ETH} echo "修改后的网卡类型为 "$MIN_BRO else echo "很遗憾,你现在不修改网卡连接类型" fi read -p "请问你是要在原先的上面怎么操作IP,如果修改请输入"y",添加请输入"n",其他他键则退出 " IpEnter if [ "$IpEnter" == y ] then read -p "请输入原先IP地址 " ORI_IP ORI_IP=$(grep -iE "IPADDR" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') read -p "请输入你要修改后的地址 " MIN_IP echo $MIN_IP|grep "^[0-9]{1,3}.([0-9]{1,3}.){2}[0-9]{1,3}$" > /dev/null if [ $? -ne 0 ] then echo "IP地址输入错误,必须全部是数字" return 1 fi ##将IP地址的每段用变量找出来 a=$(echo $MIN_IP|awk -F . '{print $1}') b=$(echo $MIN_IP|awk -F . '{print $2}') c=$(echo $MIN_IP|awk -F . '{print $3}') d=$(echo $MIN_IP|awk -F . '{print $4}') ##遍历IP地址每段的数字 for num in $a $b $c $d do if [ $num -gt 255 ] || [ $num -lt 0 ] then echo $MIN_IP"中,字段"$num"错误" return 1 fi done echo $MIN_ip "合法,可以替换成这个IP" sed -i "s#${ORI_IP}#${MIN_IP}#g" $PATH_ETH echo "现在替换的IP地址为 "${MIN_IP} elif [ "$IpEnter" == n ] then read -p "请输入你要添加的IP " addIP sed -i "6a IPADDR=${addIP}" $PATH_ETH echo "添加成功,IPADDR="${addIP} else echo "你选择退出" #continue; fi read -p "请问你是要在原先的上面怎么操作mac,如果修改请输入"y",添加请输入"n",其他他键则退出 " MacEnter if [ "$MacEnter" == y ] then read -p "请输入你原先的mac地址 " ORI_MAC ORI_MAC=$(grep -iE "NETMASK" /etc/sysconfig/network-scripts/$eth | awk -F "=" '{print $2}') read -p "请输入你要修改的mac地址 " MIN_MAC echo $MIN_MAC|grep "^[0-9]{1,3}.([0-9]{1,3}.){2}[0-9]{1,3}$" > /dev/null if [ $? -ne 0 ] then echo "IP地址输入错误,必须全部是数字" return 1 fi ##将IP地址的每段用变量找出来 a=$(echo $MIN_MAC|awk -F . '{print $1}') b=$(echo $MIN_MAC|awk -F . '{print $2}') c=$(echo $MIN_MAC|awk -F . '{print $3}') d=$(echo $MIN_MAC|awk -F . '{print $4}') ##遍历IP地址每段的数字 for num in $a $b $c $d do if [ $num -gt 255 ] || [ $num -lt 0 ] then echo $MIN_MAC"中,字段"$num"错误" return 1 fi done echo $IN_MAC "合法,可以替换成这个IP" sed -i "s#${ORI_MAC}#${MIN_MAC}#g" $PATH_ETH echo "现在替换成的MAC地址为 "$MIN_MAC elif [ "$MacEnter" == n ] then read -p "请输入你要添加的IP " addmac sed -i "6a NETMASK=${addmac}" $PATH_ETH echo "添加成功,NETMASK="${addmac} else echo "你选择退出" exit; fi } BOND_TRUE(){ read -p "请输入你要选择做bond的网卡bond " bond0 read -p "请输入你要选择做的网卡eth " bond_ip1 read -p "请输入你要选择做的网卡eth " bond_ip2 touch /etc/sysconfig/network-scripts/ifcfg-$bond0 cp -rf /etc/sysconfig/network-scripts/ifcfg-$bond0 /etc/sysconfig/network-script/bond_bakup_liangyuxing/ cp -rf /etc/sysconfig/network-scripts/ifcfg-$bond_ip1 /etc/sysconfig/network-script/bond_bakup_liangyuxing/ cp -rf /etc/sysconfig/network-scripts/ifcfg-$bond_ip2 /etc/sysconfig/network-script/bond_bakup_liangyuxing/ cp -rf /etc/modprobe.d/dist.conf /etc/sysconfig/network-script/bond_bakup_liangyuxing/ cp -rf /etc/rc.local /etc/sysconfig/network-script/bond_bakup_liangyuxing/ #if [ $# != 3 ] #then # echo "你输入服软参数不正确,请重新输入" #fi #read -p "请输入你要选择做bond的网卡bond " bond0 #read -p "请输入你要选择做的网卡eth " bond_ip1 #read -p "请输入你要选择做的网卡eth " bond_ip2 read -p "请输入网口聚合的bond0的IP " ip read -p "请输入网口聚合的bond0的mac " mac read -p "请输入网口聚合的bond0的gate " gate cat<<EOF > /etc/sysconfig/network-scripts/ifcfg-$bond0 DEVICE=$bond0 BOOTPROTO=static ONBOOT=yes USERCTL=no IPADDR=$ip NETMASK=$mac GATEWAY=$gate DNS1=114.114.114.114 EOF cat<<EOF > /etc/sysconfig/network-scripts/ifcfg-$bond_ip1 DEVICE=$bond_ip1 ONBOOT=yes BOOTPROTO=none TYPE=Ethernet MASTER=bond0 SLAVE=yes USERCTL=no EOF cat<<EOF > /etc/sysconfig/network-scripts/ifcfg-$bond_ip2 DEVICE=$bond_ip2 ONBOOT=yes BOOTPROTO=none TYPE=Ethernet MASTER=bond0 SLAVE=yes USERCTL=no EOF echo "alias bond0 bonding options bond0 mode=6 miimon=100" >> /etc/modprobe.d/dist.conf cat<<EOF >> /etc/rc.local ifenslave $bond0 $bond_ip1 $bond_ip2 EOF chkconfig NetworkManager off if [ $? -eq 0 ] then echo "bond0创建成功" fi } TEAMPWD_TRUE(){ ##查看teamviewer账号 LookTem=`teamviewer --info | grep -i "TeamViewer ID" | awk -F ":" '{print $2}'` ##判断账号是否为空 if [ ! -n "$LookTem" ] then echo "teamviewer账号查看失败,请重新查看" exit 1 fi ##判断账号是否为整数 if [ -n "`echo $LookTem | sed 's/[0-9]//g'`" ] then echo "teamviewer的账号为 "$LookTem else echo "你输入的账号不符合整数系列" fi ##修改teamviewer的并且密码加密 C_password="12345678" read -p "请问需要修改密码吗,如果需要请输入"y",不需要请输入其他 " modifypwd if [ "$modifypwd" == y ] then read -p "请输入你要修改成的teamviewer密码 " -s M_password echo "你现在使用的密码是 "$M_password teamviewer --passwd $M_password ##判断密码是否为空 if [ ! -n "$M_password" ] then echo "密码修改不可为空" exit 1 elif [ "$M_password" != "$C_password" ] then echo "密码修改成功" echo "现在的密码为 "$M_password else echo "密码修改失败" exit 1 fi else echo "你现在不需要修改密码" exit 1 fi } TIME_TRUE(){ ##获取当前服务器的时间 Current_date=`date` ##查看服务器的时间 echo "现在服务器的时间是 "$Current_date ##验证ntpdate时间服务器是否安装 ntpdate_computerID=`rpm -qa | grep ntpdate` if [ $? -eq 0 ] then echo "ntpdate服务器已安装" else yum -y install ntpdate > /dev/null 2>&1 fi ##获取北京时间 Beijing_date=`ntpdate -u cn.pool.ntp.org` ##修改时间 read -p "请输入你要修改的时间 " time date -s "$time" if [ ! -n "$time" ] then echo "你要修改的时间不可以为空" exit 1 elif [ "$time" = "$Current_date" ] then echo "现在的时间和你输入的时间是一样的,修改失败" exit 1 elif [ "$time" = "$Beijing_date" ] then echo "你输入的时间正确,当前修改的时间为 "$time ##写入bios程序里面 clock -w exit 1 else echo "你可以手动输入 "$time ntpdate -u cn.pool.ntp.org clock -w fi } ##IPTV数据库设置 Path=/opt/liangyuxing/ date=`date +%F-%T` root="root" password="123456" chmod 400 /etc/my.cnf #read -p "请输入数据库的用户是 " root #read -p "请输入数据库的密码是 " password MYSQL_TRUE(){ BEIFEN(){ if [ ! -d "$Path" ] then echo "文件不存在" /bin/mkdir -p $Path else echo "文件名/opt/liangyuxing/存在" fi read -p "请输入你使用数据库的名称 " databases; echo "开始备份选择的数据库" /usr/bin/mysqldump -u$root -p$password $databases > ${Path}${databases}_${date}.sql } TABLE(){ read -p "请输入你使用数据库的名称 " databases; read -p "请输入你要查看的表" table /usr/bin/mysql -u$root -p$password $databases -e "select * from $table" } IP(){ read -p "请输入你使用数据库的名称 " databases; read -p "请输入现在数据库的IP地址 " CurrentIP read -p "请输入修改后数据库的IP地址 " ModifyIP #updateIP="update globalconfig set value=replace(value,'192.168.100.255','192.168.3.254') where value like "%192.168.100.255%"" if [ ! -n "$CurrentIP" ] && [ ! -n "$ModifyIP" ] then echo "你输入的IP不可为空" exit 1 else /usr/bin/mysql -u$root -p$password <<EOF use $databases update globalconfig set value=replace(value,'$CurrentIP','$ModifyIP') where value like "%$CurrentIP%"; EOF fi if [ $? -eq 0 ] then echo "更换成功" else echo "更换失败" exit 1 fi } SHUAXIN(){ read -p "请输入你要刷新的IP和端口" Shuaxin echo "刷新epg接口" curl "http://$Shuaxin/api/epg/refreshcfg" echo "刷新portal"接口 curl "http://$Shuaxin/api/portal/refreshcfg" tenantID=$(curl "http://$Shuaxin/portal/api/tenant") echo "站点ID为 "$tenantID } cat<<EOF 数据库操作 --------------------------------------------------------------- --------------------------------------------------------------- && 1.备份数据库 && && 2.查看表 && && 3.修改globalconfig配置文件 && && 4.接口刷新和站点ID查看 && --------------------------------------------------------------- --------------------------------------------------------------- EOF read -p "请输入你要选择的事项" mysql case $mysql in 1) BEIFEN ;; 2) TABLE ;; 3) IP ;; 4) SHUAXIN ;; *) echo "你输入的不符合我们选择的要求" ;; esac } SMS_TRUE(){ read -p "请输入你要添加的频道ID " ID read -p "请输入你要添加的code(如果code码低于10,请在前面补0) " code read -p "请输入你要添加的IP " IP read -p "请输入你要添加的端口port " port read -p "请输入你要添加这路流指定的网卡eth " eth touch /opt/smart/cdn/sms/db/SmsLiveDb_adam_SMS.xml cat<<EOF >>/opt/smart/cdn/sms/db/SmsLiveDb_adam_SMS.xml <LiveChannelDbItem> <ChannelIndex>${ID}</ChannelIndex> <ChannelName>CCTV1</ChannelName> <CpCode>TV000000000000000000${code}@HHZT</CpCode> <ChannelType>1</ChannelType> <CtrlState>1</CtrlState> <TimeShift>1</TimeShift> <StorageDuration>120</StorageDuration> <Relay>0</Relay> <RelayPktFmtType>UDP</RelayPktFmtType> <RelayIp>236.0.0.1</RelayIp> <RelayPort>3001</RelayPort> <RelayIfName>eth0</RelayIfName> <RelayIfPort>0</RelayIfPort> <PhyChannelDbItem> <PhyCpCode>PHY00000000${code}</PhyCpCode> <PhyChannelType>UDP</PhyChannelType> <LiveSourceIp>${IP}</LiveSourceIp> <LiveSourcePort>${port}</LiveSourcePort> <Frequency>0</Frequency> <ServiceId>0</ServiceId> <PmtPid>0</PmtPid> <IfIp>0.0.0.0</IfIp> <IfName>${eth}</IfName> <IfPort>${port}</IfPort> </PhyChannelDbItem> </LiveChannelDbItem> EOF } CLEAR_TRUE(){ read -p "请问你确定要清空配置文件吗,此操作不可逆转,如果确定请按"y",返回按任意键 " clear if [ "$clear" == y ] then echo "你选择清空配置文件" >/opt/smart/cdn/sms/db/SmsLiveDb_adam_SMS.xml else echo "你选择返回,不删除配置文件" exit 1 fi } PROCESS_TRUE(){ hslicense=`ps -ef | grep "hslicense" | grep -v "grep"` >/dev/null 2>&1 lse=`ps -ef | grep "lse_server" | grep -v "grep"` >/dev/null sms=`ps -ef | grep "sms" | grep -v "grep"` >/dev/null portal=`ps -ef | grep "portal" | grep -v "grep"` >/dev/null 2>&1 epg=`ps -ef | grep "epg" | grep -v "grep"` >/dev/nulll push=`ps -ef | grep "push_agent" | grep -v "grep"` >/dev/null nginx=`ps -ef | grep "nginx" | grep -v "grep"` >/dev/null hsmonitor=`ps -ef | grep "hsmonitor" | grep -v "grep"` >/dev/null tomcat=`ps -ef | grep "tomcat.*" | grep -v "grep"` >/dev/null if [ "$hslicense" == "" ] then sleep 1; echo "hslicense进程不存在" else sleep 1; echo "hslicense进程存在" fi if [ "$lse" == "" ] then sleep 1; echo "lse进程不存在" else sleep 1; echo "lse进程存在" fi if [ "$sms" == "" ] then sleep 1; echo "sms进程不存在" else sleep 1; echo "sms进程存在" fi if [ "$portal" == "" ] then sleep 1; echo "portal进程不存在" else sleep 1; echo "portal进程存在" fi if [ "$epg" == "" ] then sleep 1; echo "epg进程不存在" else sleep 1; echo "epg进程存在" fi if [ "$push" == "" ] then sleep 1; echo "push进程不存在" else sleep 1; echo "push进程存在" fi if [ "$nginx" == "" ] then sleep 1; echo "nginx进程不存在" else sleep 1; echo "nginx进程存在" fi if [ "$hsmonitor" == "" ] then sleep 1; echo "hsmonitor进程不存在" else sleep 1; echo "hsmonitor进程存在" fi if [ "$tomcat" == "" ] then sleep 1; echo "tomcat进程不存在" else sleep 1; echo "tomcat进程存在" fi } cat<<EOF 辉视脚本模式 ----------------------------------------------------------------- ----------------------------------------------------------------- && 1.IP地址修改(主网卡) && && 2.mac地址修改(主网卡) && && 3.网关地址修改(主网卡) && && 4.连接方式修改(主网卡) && && 5.网络配置修改(其他网卡) && && 6.bond0设置 && && 7.teamviewer账号密码信息查看 && && 8.服务器时间设置 && && 9.数据库操作 && && 10.sms流媒体的配置 && && 11.清空内容(将所有的sms配置内容清空) && && 12.辉视软件进程查看 && ----------------------------------------------------------------- ----------------------------------------------------------------- EOF read -p "请输入你要修改的事项 " things case $things in 1) IP_TRUE ;; 2) MASK_TRUE ;; 3) GATEWAY_TRUE ;; 4) BRO_TRUE ;; 5) ETH_TRUE ;; 6) BOND_TRUE ;; 7) TEAMPWD_TRUE ;; 8) TIME_TRUE ;; 9) MYSQL_TRUE ;; 10) SMS_TRUE ;; 11) CLEAR_TRUE ;; 12) PROCESS_TRUE ;; *) echo "你输入的范围不在选择范围之间,请重新输入" ;; esac