当前位置: 首页 > news >正文

day58-Shell编程(第四部分)

1.每日复盘与今日内容

1.1复盘

  • 字符串比对、正则(=~)
  • if判断(while、break、continue)🍟🍟🍟🍟🍟
  • 一、二级菜单

1.2今日内容

  • case语句
  • for循环

2.case语句

语法结构:
case $1 in			# $1脚本的第一个参数匹配序列1)命令集合;;匹配序列2)命令集合;;匹配序列3)命令集合;;*) 无匹配后续命令
esac案例1.
[root@shell day04]# cat case.sh 
#!/bin/bash
case $1 inshell)echo shell.....;;mysql)echo mysql.....;;docker)echo docker....;;*)echo "Usage: $0 [shell|mysql|docker]"
esac案例2.系统信息
[root@shell day04]# cat case.sh 
#!/bin/bash
men(){echo -e "\t\t\t\t\t\033[34m1|f 显示登陆信息 \033[0m"echo -e "\t\t\t\t\t\033[34m2|d 显示磁盘信息 \033[0m"echo -e "\t\t\t\t\t\033[34m3|m 显示内存信息 \033[0m"echo -e "\t\t\t\t\t\033[34m4|u 显示负载信息 \033[0m"echo -e "\t\t\t\t\t\033[34m5|q 退出程序 \033[0m"
}
men
while true
do
read -p "请输入查看系统信息的编号[h菜单]: " num
case $num in1|f)w;;2|d)df -h;;3|m)free -h;;4|u)uptime;;5|q)exit;;h)men;;*)echo "Usage: $0 [1|2|3|4|5]"
esac
done案例3.nginx启动脚本、程序是没有被systemctl所管理的 类似python程序 java程序 jar
nginx启动方式:  所有的服务启停都是基于二进制的命令 /bin
systemctl start nginx
systemctl方式启停
直接调用二进制命令启停 查看命令所在的路径which
[root@shell ~]# which nginx
/usr/sbin/nginx
[root@shell ~]# /usr/sbin/nginx          # 启动
[root@shell ~]# /usr/sbin/nginx -s stop  # 停止
[root@shell ~]# /usr/sbin/nginx -s reload # 重新加载
invalid option  # 无效的选项
[root@shell ~]# /usr/sbin/nginx -s stop && sleep 2 && /usr/sbin/nginx # 重启
[root@shell ~]# netstat -tnulp|grep 80|awk '{print $4}' # 查看当前状态#nginx启停脚本框架
[root@shell day04]# cat nginx.sh 
#!/bin/bash
case $1 instart)/usr/sbin/nginx;;stop)/usr/sbin/nginx -s stop;;reload)/usr/sbin/nginx -s reload;;restart)/usr/sbin/nginx -s stop && sleep 1 && /usr/sbin/nginx;;status)if [ `netstat -tnulp|grep 80|awk '{print $4}'|wc -l` -eq 1  ];thenecho "Nginx运行中"elseecho "Nginx停止运行"fi;;*)echo "Usage: $0 [start|stop|restart|reload|status]"
esac#优化后的nginx启停脚本
[root@shell day04]# cat nginx.sh 
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
#env
NGINX=/usr/sbin/nginx#
a=$1
Test(){if [ $? -eq 0 ];thenaction "Nginx $a"  trueelse    action "Nginx $a"  falsefi
}case $1 instart)$NGINX &>/dev/nullTest;;stop)$NGINX -s stop &>/dev/nullTest;;reload)$NGINX -s reload &>/dev/nullTest;;restart)$NGINX -s stop && sleep 1 && $NGINX &>/dev/nullTest;;status)if [ `netstat -tnulp|grep 80|awk '{print $4}'|wc -l` -eq 1  ];thenecho "Nginx运行中"elseecho "Nginx停止运行"fi;;*)echo "Usage: $0 [start|stop|restart|reload|status]"
esac案例4.基于case的jumpserver跳板机案例
#跳板机服务器和后端服务器做免秘钥
跳板机: 10.0.0.71
[root@shell ~]# ssh-keygen
[root@shell ~]# ssh-copy-id 172.16.1.8
[root@shell ~]# ssh-copy-id 172.16.1.7
后端主机: 172.16.1.7 172.16.1.8#跳板机框架
[root@shell day04]# cat jumpserver.sh
#!/bin/bash
web01=172.16.1.7
web02=172.16.1.8
nfs=172.16.1.31
mysql=172.16.1.51
backup=172.16.1.41hos(){
echo -e "\t\t\t\t\t\033[34m1.$web01 \033[0m"
echo -e "\t\t\t\t\t\033[34m2.$web02 \033[0m"
echo -e "\t\t\t\t\t\033[34m3.$nfs \033[0m"
echo -e "\t\t\t\t\t\033[34m4.$mysql \033[0m"
echo -e "\t\t\t\t\t\033[34m5.$backup \033[0m"
}
hoswhile true
doread -p "请输入要连接主机的编号[h主菜单]: " numcase $num in1)ssh $web01;;2)ssh $web02;;h)clearhos;;*)echo "Usage $0 [1|2|3]"esac
done#找回密码 邮箱 微信号
#给邮箱微信号发送验证码
#输入验证码如果相等、则发送密码给到邮箱 微信[root@shell day04]# cat jumpserver.sh
#!/bin/bash
web01=172.16.1.7
web02=172.16.1.8
nfs=172.16.1.31
mysql=172.16.1.51
backup=172.16.1.41role(){echo -e "\t\t\t\t\t\033[35m1.运维 \033[0m"echo -e "\t\t\t\t\t\033[35m2.开发 \033[0m"
}
roleops(){
echo -e "\t\t\t\t\t\033[34m1.$web01 \033[0m"
echo -e "\t\t\t\t\t\033[34m2.$web02 \033[0m"
echo -e "\t\t\t\t\t\033[34m3.$nfs \033[0m"
echo -e "\t\t\t\t\t\033[34m4.$mysql \033[0m"
echo -e "\t\t\t\t\t\033[34m5.$backup \033[0m"
}dev(){
echo -e "\t\t\t\t\t\033[34m1.$web01 \033[0m"
}trap "" INT TSTP HUP
while true
doread -p "请输入你的角色编号: " num# 密码控制while truedowhile truedolet i++if [ $i -gt 4 ] ;thenecho "1.重新输入密码"echo "2.找回密码"read -p "请输入1|2: " num1if [ $num1 -eq 1 ];thenbreakelif  [ $num1 -eq 2 ];then read -p "请输入邮箱地址:" maran=`echo $((RANDOM))|md5sum|cut -c1-8`echo $ran|mail -s "验证码" "$ma"while truedoread -p "请输入邮箱接收到的验证码: " mif [ $ran = $m ];thenecho oldboy123|mail -s "jumpserver运维密码" "$ma" &>/dev/nullecho "已将密码发送至邮箱请查收!"break 2elif [ $ran != $m ];thenecho "请重新验证码"continuefidonefielsebreakfidoneread -s -p "欢迎lnb运维请输入您的密码: " passecho -e "\n"if [ $pass = oldboy123 ];thenbreakelseecho "密码错误、重新输入"continuefidone#判断用户输入的角色if [ $num -eq 1 ];thenopswhile truedoread -p "请输入要连接主机的编号[h主菜单]: " num1case $num1 in1)ssh $web01;;2)ssh $web02;;h)clearops;;woshiyunwei)exit;;*)echo "Usage $0 [1|2|3]"esacdoneelif [ $num -eq 2 ];thendevwhile truedoread -p "请输入要连接主机的编号[h主菜单]: " num1case $num1 in1)ssh $web01;;h)cleardev;;*)echo "Usage $0 [1|2|3]"esacdonefidone

3.for循环

for i in  取值列表  # 取值列表以空格分隔进行取值 i为变量名称 数字、字符串、支持命令、序列
do命令集合
done#案例1.支持数字
[root@shell day04]# cat for.sh
#!/bin/bash
for i in 1 2 3
doecho $i
done
[root@shell day04]# sh for.sh
1
2
3#案例2.支持字符串
[root@shell day04]# cat for.sh
#!/bin/bash
for i in a b c d
doecho $i
done
[root@shell day04]# sh for.sh
a
b
c
d#案例3.支持序列
[root@shell day04]# cat for.sh
#!/bin/bash
for i in {a..d}
doecho $i
done
[root@shell day04]# sh for.sh
a
b
c
d[root@shell day04]# cat for.sh
#!/bin/bash
for i in {01..10}
doecho $i
done
[root@shell day04]# sh for.sh
01
02
03
04
05
06
07
08
09
10#案例4.支持命令
[root@shell day04]# cat for.sh
#!/bin/bash
for i in `ls`
doecho $i
done
[root@shell day04]# sh for.sh
case.sh
for.sh
jumpserver.sh
nginx.sh[root@shell day04]# cat 1.txt 
aa
bb
cc
[root@shell day04]# cat for.sh 
#!/bin/bash
for i in `cat 1.txt`
doecho $i
done
[root@shell day04]# sh for.sh 
aa
bb
cc#案例5.可以利用循环次数做和值无关的事情
[root@shell day04]# cat for.sh
#!/bin/bash
for i in  a b c
doecho hehe
done
[root@shell day04]# sh for.sh
hehe
hehe
hehe[root@shell day04]# cat for.sh
#!/bin/bash
for i in  a b c
dolet a++
done
echo 循环了 $a 次
[root@shell day04]# sh for.sh
循环了 3 次#案例6.变量和字符串的拼接
[root@shell day04]# cat for.sh
#!/bin/bash
for i in  a b c
doecho oldboy$i
done
[root@shell day04]# sh for.sh
oldboya
oldboyb
oldboyc[root@shell day04]# cat for.sh
#!/bin/bash
for i in  1 2 3
doecho oldboy$i
done
[root@shell day04]# sh for.sh
oldboy1
oldboy2
oldboy3#案例7.批量创建10个用户 oldboy01 -oldboy10
[root@shell day04]# cat for.sh
#!/bin/bash
for i in  1 2 3
douseradd oldboy$i
done
[root@shell day04]# sh for.sh
[root@shell day04]# tail -3 /etc/passwd
oldboy1:x:1001:1001::/home/oldboy1:/bin/bash
oldboy2:x:1002:1002::/home/oldboy2:/bin/bash
oldboy3:x:1003:1003::/home/oldboy3:/bin/bash[root@shell day04]# cat for.sh 
#!/bin/bash
read -p "请输入用户名称的前缀: " prefix
read -p "请输入创建用户的个数: " num
for i in  `seq $num`
douseradd $prefix$i
done[root@shell day04]# cat for.sh 
#!/bin/bash
read -p "请输入用户名称的前缀: " prefix
#判断不能为空或者必须为字符串read -p "请输入创建用户的个数: " num
#判断必须是整数for i in  `seq $num`
doecho $prefix$i
doneread -p "创建或者删除以上用户[y|d] " re
for i in `seq $num`
doif [ $re = y ];thenuser=$prefix$iid $user &>/dev/nullif [ $? -ne 0 ];then useradd $user && echo $user 创建成功elseecho $user已存在fielif [ $re = d ];thenuser=$prefix$iid $user &>/dev/null[ $? -eq 0 ] && userdel -r $user && echo $user 删除成功fi
done#案例7.创建不同的用户
[root@shell day04]# cat user.sh
#!/bin/bash
for i in `cat 1.txt`
douseradd $i
done
[root@shell day04]# cat 1.txt 
oldboy
oldgirl
linux
[root@shell day04]# sh user.sh 
[root@shell day04]# tail -3 /etc/passwd
oldboy:x:1006:1006::/home/oldboy:/bin/bash
oldgirl:x:1007:1007::/home/oldgirl:/bin/bash
linux:x:1008:1008::/home/linux:/bin/bash#在命令行执行for循环
for i in 取值列表;do 命令集合;done
[root@shell day04]# for i in {1..3};do echo $i;done
1
2
3#创建用户
[root@shell day04]# for i in {1..3};do useradd oldboy$i;done
[root@shell day04]# tail -3 /etc/passwd
oldboy1:x:1009:1009::/home/oldboy1:/bin/bash
oldboy2:x:1010:1010::/home/oldboy2:/bin/bash
oldboy3:x:1011:1011::/home/oldboy3:/bin/bash#删除用户
[root@shell day04]# for i in {1..3};do userdel -r oldboy$i;done#案例8.批量探测网络内10.0.0.0/24哪些主机在线 笔试题
10.0.0.1-10.0.0.254 哪些主机可以ping通
[root@shell day04]# cat ping.sh 
#!/bin/bash
for i in `seq 254`
do{ip=10.0.0.$iping -c1 -W1  $ip &>/dev/null[ $? -eq 0 ] && echo $ip 在线} &
done
wait

4.今日总结

  • case语句
  • for循环

http://www.dtcms.com/a/553425.html

相关文章:

  • 用AI写了一个文档拼音标注工具 中文+拼音一键生成
  • 做网站还有意义同样是div 怎么有些网站收录少 有些多
  • 企必搜做网站做国际物流在哪些网站找客户
  • 移动端适配完全指南:从基础到最佳实践
  • 使用JMeter进行API性能压测(执行篇)
  • IntelliJ IDEA 远程调试(Remote Debugging)教程
  • 网站服务器++免费做电子手抄报的网站
  • 单页网站的优点网络公司是做什么的?
  • 阿瓦隆 Q 90T矿机:低功耗高效挖矿,是否值得选择?
  • 印度实时股票数据源接口对接文档-IPO新股、k线数据
  • HTTPS接口国密安全设计(含防重放设计)
  • 网站设计公司(信科网络)中国制造网外贸平台怎么注册
  • 网站模版如何去除title版权信息499元做网站
  • 武进建设局网站首页胖鼠wordpress
  • 机器学习第一阶段
  • Linux内核RDMA用户态内存映射机制深度解析:零拷贝高性能的基石
  • 组态软件和实时数据库区别大吗?
  • SpringBoot】Spring Boot 项目的打包配置
  • 递归专题5 - FloodFill算法专题
  • 系统架构设计师论文-论软件架构的复用
  • 沙市做网站weiswordwordpress微信登录设置
  • 理解MySQL的原理
  • Mac通过命令行开启ssh服务
  • 哈尔滨有哪些做网站的公司站长工具seo综合查询问题
  • 珠海做网站的wordpress 写作
  • 【计算机基础】之核心架构
  • 临西网站建设公司公司核名查询官网
  • PPIO独家上新GPU实例模板,一键部署Kimi-Linear
  • 工业级电池健康管理利器:GRX-3000 系列电池诊断站技术解析
  • 旅游网站建设功能意义wordpress 模板 免费