for 语法
for 变量 in 元素 do 做什么 done [root@shell ~]# cat for.sh #!/bin/sh for i in I am ajie teacher I am 18 do echo $i done
获取每个单词的长度
[root@shell ~]# cat for.sh #!/bin/sh for i in I am lizhenya teacher I am 18 do echo ${#i} done
如何做比较 取出单词长度小于3
[root@shell ~]# cat for.sh #!/bin/sh for i in I am lizhenya teacher I am 18 do [ ${#i} -lt 3 ] && echo $i done [root@shell ~]# sh for.sh I am I am 18 [root@shell ~]# echo $name|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}' I am I am 18
if 语法
[]两边都有空格
if [ 表达式 ];then 执行的命令 fi if [ 如果你有房子 ];then 我就嫁给你 fi if第二种书写方式 if [ 表达式 ] then 执行的命令 fi
if 单分支
if [ -f /etc/passwd ] then echo ok fi if [ 10 -eq 10 ] then echo ok fi if [ "root" =~ ^r ] then echo ok fi
if 双分支结构
if [ 10 -eq 10 ] then echo 成立 else echo 不成立 fi
多分支结构 多个条件
if [ $1 -lt 20 ] then echo 小了 elif [ $1 -gt 20 ] echo 大了 else echo 相等 fi
案例1: 输入两个数字 使用if判断来比较两个数字的大小
[root@shell day3]# cat diff.sh #!/bin/sh if [ $# -ne 2 ];then echo "请输入两个数字" exit 2 fi if [[ "$1" =~ ^[0-9]+$ && "$2" =~ ^[0-9]+$ ]];then if [ $1 -gt $2 ];then echo "$1>$2" elif [ $1 -lt $2 ];then echo "$1<$2" else echo "$1=$2" fi else echo "请输入整数" exit 4 fi
案例2:根据操作系统版本号安装不同的YUM源
[root@shell ~]# cat yum.sh #!/bin/bash #Author:ajie #Time:2020-04-11 11:17:50 Centos=`cat /etc/redhat-release |awk '{print $4}'|awk -F. '{print $1}'` which wget &>/dev/null if [ $? -ne 0 ] then echo "yum installed ..." yum install wget -y &>/dev/null if [ $? -eq 0 ] then echo "wget installed..." else echo "internet is not found.." fi if [ $Centos = "7" ] then wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null if [ $? -eq 0 ] then echo "yum is ok" else echo "yum is error plaese check your network" fi else wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null if [ $? -eq 0 ] then echo "yum is ok" else echo "yum is error please check your network" fi fi fi
案例3:根据需求安装不同的版本服务
[root@shell ~]# cat /server/scripts/login.sh #!/bin/bash a7 () { echo -e "\t\t\t\t\t--------------" echo -e "\t\t\t\t\t1.运维人员登录" echo -e "\t\t\t\t\t2.开发人员登录" echo -e "\t\t\t\t\t--------------" } yunwei () { echo -e "\t\t\t\t\twelcome 运维" echo -e "\t\t\t\t\t1.install PHP" echo -e "\t\t\t\t\t2.install Nginx" echo -e "\t\t\t\t\t3.install MySQL" echo -e "\t\t\t\t\t4.install Java" echo -e "\t\t\t\t\t5.install Python" echo -e "\t\t\t\t\t6.install Docker" echo -e "\t\t\t\t\t7.install K8s" } kaifa () { echo -e "\t\t\t\t\twelcome 开发" echo -e "\t\t\t\t\t1.install PHP" echo -e "\t\t\t\t\t2.install MySQL" echo -e "\t\t\t\t\t3.install Java" echo -e "\t\t\t\t\t4.install Python" } a1 () { echo -e "\t\t\t\t\t1.install PHP5.5" echo -e "\t\t\t\t\t2.install PHP5.6" echo -e "\t\t\t\t\t3.install PHP5.7" } a2 () { echo -e "\t\t\t\t\t1.install MySQL5.5" echo -e "\t\t\t\t\t2.install MySQL5.6" echo -e "\t\t\t\t\t3.install MySQL5.7" } a3 () { echo -e "\t\t\t\t\t1.install JDK8" echo -e "\t\t\t\t\t3.install JDK9" echo -e "\t\t\t\t\t2.install JDK10" echo -e "\t\t\t\t\t3.install JDK11" echo -e "\t\t\t\t\t4.install JDK12" } a4 () { echo -e "\t\t\t\t\t1.install Python3.4.2" echo -e "\t\t\t\t\t3.install Python3.5.2" echo -e "\t\t\t\t\t2.install Python3.6.2" echo -e "\t\t\t\t\t3.install Python3.7.2" } a7 while true do read -p "Choose your login: " num if [ $num -eq 1 ];then read -p "Input your psaaword: " passwd if [ "$passwd" == "yunwei" ];then yunwei fi read -p "Select the corresponding operation: " number if [ $number -eq 1 ];then echo "install PHP..." && a7 elif [ $number -eq 2 ];then echo "install Nginx..." && a7 elif [ $number -eq 3 ];then echo "install MySQL..." && a7 elif [ $number -eq 4 ];then echo "install Java..." && a7 elif [ $number -eq 5 ];then echo "install Python..." && a7 elif [ $number -eq 6 ];then echo "install Docker..." && a7 elif [ $number -eq 7 ];then echo "install K8s..." && a7 else echo "no server " && exit fi elif [ $num -eq 2 ];then read -p "Input your psaaword: " passwd if [ "$passwd" == "kaifa" ];then kaifa fi read -p "Choose your login: " number if [ $number -eq 1 ];then a1 read -p "Choose your server: " n if [ $n -eq 1 ];then echo "install PHP5.5..." && a7 elif [ $n -eq 2 ];then echo "install PHP5.6..." && a7 elif [ $n -eq 3 ];then echo "install PHP5.7..." && a7 fi elif [ $number -eq 2 ];then a2 read -p "Choose your server: " n if [ $n -eq 1 ];then echo "install MySQL5.5..." && a7 elif [ $n -eq 2 ];then echo "install MySQL5.6..." && a7 elif [ $n -eq 3 ];then echo "install MySQL5.7..." && a7 fi elif [ $number -eq 3 ];then a3 read -p "Choose your server: " n if [ $n -eq 1 ];then echo "install JDK8..." && a7 elif [ $n -eq 2 ];then echo "install JDK9..." && a7 elif [ $n -eq 3 ];then echo "install JDK10..." && a7 elif [ $n -eq 2 ];then echo "install JDK11..." && a7 elif [ $n -eq 3 ];then echo "install JDK12..." && a7 fi elif [ $number -eq 4 ];then a4 read -p "Choose your server: " n if [ $n -eq 1 ];then echo "install Python3.4.2" && a7 elif [ $n -eq 2 ];then echo "install Python3.5.2" && a7 elif [ $n -eq 3 ];then echo "install Python3.6.2" && a7 elif [ $n -eq 2 ];then echo "install Python3.7.2" && a7 fi else exit fi else echo "The password is error" && exit fi done # 此脚本里xxx(){} 是函数 下面会说到。
while的使用语法
while true do 代码块 done
案例:
猜数字游戏
使用命令随机生成一个数字 1-100 echo $((RANDOM%100+1)) while true do 要求用户输入要猜的数字 (数字需要判断是否是整数) read -p xxx num 比对随机数和用户猜的数字 输出结果 用户猜的数字小 则输出到屏幕 你猜的数字小了 如果数字大于随机数 则输出 你猜的数字大了 如果数字相等 则提示 恭喜你 猜对了 && exit done
[root@shell ~]# cat ran.sh #!/bin/sh ran=`echo $((RANDOM%100+1))` while true do let i++ read -p "请输入你要猜的数字[1-100]: " num if [ $num -gt $ran ];then echo "你输入的数字 $num 大了" elif [ $num -lt $ran ];then echo "你输入的数字 $num 小了" else echo "恭喜你答对了随机数为$ran" echo "总共猜了$i 次" exit fi done
流程控制语句case
case 变量 可以是直接传参 赋值 read读入 case 变量 in 模式1) 命令序列 ;; 模式2) 命令序列 ;; 模式3) 命令序列 ;; *) 无匹配后命令序列 esac
案例:
#!/bin/sh cat<<EOF 1.NFS 2.MySQL 3.Redis 4.DOCKER 5.KVM 6.退出脚本 EOF while true do read -p "请输入你想要安装的服务编号或者服务名称: " num case $num in 1) echo "yum -y instll NFS........" ;; 2) echo "yum -y install MySQL......" ;; 3) echo "yum -y install Redis......." ;; 6) exit ;; *) echo "USAGE: $0 read -p [NFS|MySQL|Redis|DOCKER|KVM]" esac done
批量创建用户:
#!/bin/sh read -p "请输入用户的前缀名称: " prefix [ -z $prefix ] && echo "请输入用户的前缀" && exit read -p "请输入要创建多少个:" num if [[ ! $num =~ ^[0-9]+$ ]];then echo "请输入整数" exit fi for i in `seq $num` do echo ${prefix}$i done read -p "你确定要创建以上用户吗?[y|yes|Y|YES|n|N|NO]: " re for a in `seq $num` do case $re in y|Y|YES|yes) useradd ${prefix}$a &>/dev/null if [ $? -eq 0 ];then echo "${prefix}$a 创建成功" else echo "useradd: user ${prefix}$a already exists" fi ;; n|N|no|NO) exit ;; *) echo "USAGE: $0 请输入[y|n]" esac done
批量删除用户:
#!/bin/sh read -p "请输入用户前缀名称: " prefix read -p "请输入用户个数: " num for i in `seq $num` do echo $prefix$num done read -p "你确定要删除以上用户吗?[y|Y|n|N]: " re for a in `seq $num` do case $re in y|Y) userdel -r $prefix$a &>/dev/null if [ $? -eq 0 ];then echo "$prefix$a 删除成功" else echo "$prefix$a 删除失败" fi ;; n|N) echo "玩啥" exit ;; *) echo "USAGE $0 [y|Y|n|N]" esac done
案例:
使用case语句写一个可查看系统资源的脚本
#!/bin/sh menu(){ cat<<EOF 1. 查看内存 2. 查看磁盘 3. 查看负载 4. 查看登陆信息 5. 查看菜单 6. 退出 EOF } menu while true do read -p "请输入查看系统信息的编号:[1-5(6:查看菜单)]: " num case $num in 1) clear free -h ;; 2) clear df -h ;; 3) clear uptime ;; 4) clear w ;; 5) clear menu ;; 6) exit ;; *) echo "USAGE $0 请按照规则来办事" esac done
Nginx服务启停
/usr/sbin/nginx # 启动 /usr/sbin/nginx -s stop # 停止 /usr/sbin/nginx -s reload # 重新加载 /usr/sbin/nginx -s stop && /usr/sbin/nginx # 先停止 后启动--> 重启 通过三剑客过滤Nginx状态 PID 端口 输出到屏幕 # 查看Nginx状态 #!/bin/sh if [ $# -eq 1 ];then pl=$1 TEST(){ if [ $? -eq 0 ];then action "Nginx $pl Is" /bin/true else action "Nginx $pl Is" /bin/false fi } START(){ if [ `ps axu|grep nginx|grep master|wc -l` -gt 0 ];then echo "Nginx is running....." exit else nginx -t &>/dev/null if [ $? -eq 0 ];then /usr/sbin/nginx else echo "请检查Nginx配置文件" exit fi fi } 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 [ `ps axu|grep nginx|grep master|wc -l` -eq 0 ];then echo "Nginx服务目前是停止状态无法查看" exit else if [ `curl -sI http://127.0.0.1|awk 'NR==1{print $2}'` -eq 200 ];then action "Nginx is running....." /bin/true else action "Nginx is error......" /bin/false fi fi } USAGE(){ echo "USAGE $0 [start|stop|reload|restart|status]" } [ -f /etc/init.d/functions ] && . /etc/init.d/functions case $1 in start) START TEST ;; stop) STOP TEST ;; reload) RELOAD TEST ;; restart) RESTART TEST ;; status) STATUS ;; *) USAGE esac else echo "输入一个就行" exit fi
简陋JUMPSERVER跳板机
1.职责 运维 开发测试 权限
2.菜单 可以连接的服务器有哪些
3.免秘钥登陆
4.开发把脚本ctrl+c掉 直接ssh登陆数据库删除 跑路 禁止ctrl+c z 不能让脚本退出
5.运维口令
弟弟版
#!/bin/sh WEB01=10.0.0.7 WEB02=10.0.0.8 NFS=10.0.0.31 menu(){ cat<<EOF 1.WEB01 2.WEB02 3.NFS 4.菜单 EOF } menu trap "echo 不要瞎操作,小心炸掉你" HUP INT TSTP while true do read -p "请输入你要连接的服务器编号或者是主机名:[WEB01|1|WEB0|2] " num case $num in 1) ssh root@$WEB01 ;; 2) ssh root@$WEB02 ;; 3) ssh root@$NFS ;; 4) clear menu ;; woshiyunwei) echo "客官慢走.....下次在来" exit ;; *) read -p "请输入你要连接的服务器编号或者是主机名:[WEB01|1|WEB0|2] " esac done
完整版:
#!/bin/sh JD_menu(){ cat<<EOF 1.我是运维 2.我是开发 3.啥也不是 EOF } OP_menu(){ cat<<EOF 1. WEB01 2. WEB02 3. MySQL 4. NFS 5. Redis 6. ZABBIX 7. ELK 8. Git 9. Jenkins EOF } DEV_menu(){ cat<<EOF 1.WEB01 2.WEB02 3.Git EOF } JD_menu while true do read -p "请输入你的职责: " jd trap "" INT TSTP HUP while true do case $jd in 1) if [ $jd -eq 1 ];then read -p "请输入运维的暗号: " pass if [ "$pass" = "woshiyunwei" ];then OP_menu read -p "请输入你要连接的服务器编号: " num case $num in 1) ssh root@10.0.0.7 ;; 2) ssh root@10.0.0.8 ;; *) echo "输入服务器编号" esac else read -p "不要假冒运维,请重新输入你的职责" break fi fi ;; 2) DEV_menu read -p "请输入你要连接的服务器编号: " num case $num in 1) ssh root@10.0.0.7 ;; 2) ssh root@10.0.0.8 ;; *) echo "输入服务器编号" esac ;; 3) read -p "请输入你的职责: " jd ;; woxiangtuichu) exit ;; *) read -p "必须输入身份" break esac done done