美文网首页
shell脚本循环

shell脚本循环

作者: 天生顽皮 | 来源:发表于2021-06-29 21:40 被阅读0次

1.case循环

case 变量 in     变量 支持传参  赋值  read读入
       模式1)
            执行的命令合集
        ;;
       模式2)
            执行的命令合集
        ;;
        模式3)
            执行的命令合集
        ;;
        *)
            执行的命令合集
esac
[root@shell /server/scripts/day4]# cat case.sh
#!/bin/sh
while true
do
read -p "请输入点东西:[shell|mysql|docker] " num
case $num in
        shell)
             echo "shells............"
         clear
        ;;
        mysql)
            echo "mysql................"
        echo hehe
        ;;c
    docker)
        echo "Docker..............."
    ;;
        *)
    echo "USAGE: $0 [shell|mysql|docker]"
esac
done


[root@shell /server/scripts/day4]# cat case.sh 
#!/bin/sh
echo "1. 汉堡"
echo "2. 水饺"
while true
do
read -p "请输入点东西:[shell|汉堡|mysql|docker] " num
case $num in
        1|汉堡)
             echo "汉堡............"
        ;;
        2|水饺)
            echo "水饺................"
        echo hehe
        ;;
    docker)
        echo "Docker..............."
    ;;
        *)
    echo "USAGE: $0 [shell|mysql|docker]"
esac
done
1.2使用case写一个nginx的启动脚本
root@shell /server/scripts/day4]# cat start_nginx.sh 
#!/bin/sh
case $1 in
    start)
         /usr/sbin/nginx
    ;;
    stop)
        /usr/sbin/nginx -s stop
    ;;
    restart)
        /usr/sbin/nginx -s stop
        sleep 1
        /usr/sbin/nginx
        ;;
        reload)
        /usr/sbin/nginx -s reload
    ;;
    status)
        Port=`netstat -tnulp|grep nginx|grep master|awk '{print $4}'|head -1`
        Pid=`ps axu|grep nginx|grep master|awk '{print $2}'`
            echo "当前Nginx的端口号: " $Port
            echo "当前Nginx的PID: " $Pid
    ;;
    *)
    echo "USAGE: $0 [start|stop|restart|reload|status]"
esac

##增加判断
[root@shell /server/scripts/day4]# cat start_nginx.sh 
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
case $1 in
    start)
         pid=`ps axu|grep nginx|grep master|awk '{print $2}'|wc -l`
             if [ $pid -eq 0 ];then
             /usr/sbin/nginx
                 if [ $? -eq 0 ];then
                    action "Nginx is $1"   /bin/true
                 fi
         else
                echo  "Nginx已经在运行中........."
             fi
    ;;
    stop)
        /usr/sbin/nginx -s stop
        if [ $? -eq 0 ];then
            action "Nginx is $1"   /bin/true
            fi
    ;;
    restart)
        /usr/sbin/nginx -s stop
        sleep 1
        /usr/sbin/nginx
        if [ $? -eq 0 ];then
            action "Nginx is $1"   /bin/true
            fi
        ;;
        reload)
        /usr/sbin/nginx -s reload
        if [ $? -eq 0 ];then
            action "Nginx is $1"   /bin/true
            fi
    ;;
    status)
        Port=`netstat -tnulp|grep nginx|grep master|awk '{print $4}'|head -1`
        Pid=`ps axu|grep nginx|grep master|awk '{print $2}'`
            echo "当前Nginx的端口号: " $Port
            echo "当前Nginx的PID: " $Pid
    ;;
    *)
    echo "USAGE: $0 [start|stop|restart|reload|status]"
esac        

2.使用脚本写一个jumpserver跳板机

1.服务器的IP地址  使用菜单输出到屏幕
2.使用case传参的方式进行连接
WEB01=10.0.0.7
WEB02=10.0.0.8
WEB03=10.0.0.9
MySQL=10.0.0.51
NFS=10.0.0.31
BACKUP=10.0.0.41

read -p "请输入你要连接服务器的编号或者IP地址: " num 
case $num in
          1|WEB01|10.0.0.7)
                          ssh root@$WEB01
                          ;;
          2|WEB02|10.0.0.8)
                          ssh root@$WEB02
                          ;;
[root@shell /server/scripts/day4]# cat jumpserver.sh 
#!/bin/bash
WEB01=10.0.0.7
WEB02=10.0.0.8
WEB03=10.0.0.9
MySQL=10.0.0.51
NFS=10.0.0.31
BACKUP=10.0.0.41
trap "echo 不要乱按小心爆炸" HUP INT TSTP
while true
do
au(){
cat<<EOF
          1.运维
          2.开发
EOF
}
au
OPS(){
cat<<EOF
            1.WEB01=10.0.0.7
            2.WEB02=10.0.0.8
            3.WEB03=10.0.0.9
            4.MySQL=10.0.0.51
            5.NFS=10.0.0.31
            6.BACKUP=10.0.0.41
                7.显示菜单
                8.返回上一级菜单
            9.休息一下
EOF
}
DEV(){
cat<<EOF
                        1.WEB01=10.0.0.7
                        2.BACKUP=10.0.0.41
                3.显示菜单
EOF
}

read -p "请输入你的身份你是运维还是开发请选择:[1:2] " num
if [ $num -eq 1 ];then
   while true
   do
       read -s -p "请输入运维的密码: " pass
           let i++
           if [ $i -eq 4 ];then
               echo "密码输入错误次数过多 2秒后重试"
           sleep 2
               #read -p "请输入邮箱找回密码: " p
              # if [ `grep -w $p|mail.txt|wc -l` -eq 1  ];then
           #  echo "oldboy123! > sendmail $p"
               #fi
           fi
       if [ $pass = "oldboy123!" ];then
        while true
        do
           OPS
           read -p "请输入你要连接服务器的编号或者IP地址: " num1
           case $num1 in
                  1)
                ssh root@$WEB01
                  ;;
                  2)
                ssh root@$WEB02
                  ;;
                  woshiyunwei)
                exit
                  ;;
                  8)
                break 2
                  ;;
                  9)
                sh /server/scripts/game.sh
                  ;;
                  *)
           esac
        done
        else
        echo "密码输入错误请重新输入"
        continue
        fi
    done
else
   while true
   do
       read -s -p "请输入开发的密码: " pass
           let i++
           if [ $i -eq 4 ];then
               echo "密码输入错误次数过多 2秒后重试"
           sleep 2
               #read -p "请输入邮箱找回密码: " p
              # if [ `grep -w $p|mail.txt|wc -l` -eq 1  ];then
           #  echo "oldboy123! > sendmail $p"
               #fi
           fi
       if [ $pass = "123!" ];then
        while true
        do
           DEV
           read -p "请输入你要连接服务器的编号或者IP地址: " num1
           case $num1 in
                  1)
                ssh root@$WEB01
                  ;;
                  2)
                ssh root@$WEB02
                  ;;
                  woshikaifa)
                exit
                  ;;
                  *)
           esac
        done
        else
        echo "密码输入错误请重新输入"
        continue
        fi
    done
fi
done

3.expect 编程语言

需要安装命令
yum -y install expect

1.expect 以.exp 或者 .ex结尾
2.set    设置变量 
  set IP 10.0.0.7
  set USER root
  set password 1
3.set timeout 设置超时时间
4.interact expect交给用户 可以继续执行命令
5.如果使用脚本调用expect 需要使用cat
spawn               交互程序开始后面跟命令或者指定程序 ssh  ssh-copy  su -
expect              获取匹配信息匹配成功则执行expect后面的程序动作
send exp_send       用于发送指定的字符串信息
exp_continue        在expect中多次匹配就需要用到
send_user           用来打印输出 相当于shell中的echo
exit                退出expect脚本
eof                 expect执行结束 退出
set                 定义变量
puts                输出变量
set timeout         设置超时时间

expect<<EOF
expect {
}
EOF
[root@shell ~]# cat expect.ex 
#!/usr/bin/expect
spawn ssh root@10.0.0.7
expect {
     "yes/no" { send "yes\r"; exp_continue }
     "password:" { send "1\n" };
}
interact

4.for循环

语法结构:
for 变量 in  值
do 
    执行的命令
done

变量 自定义的名称 i  a b
值   数字 字符串 变量  命令
取值的过程就是按照空格分隔
#!/bin/sh
for i in 1 2 3 4
do
    echo $i
done
4.1案例1
[root@shell /server/scripts/day4]# cat for.sh
#!/bin/sh
for i in shell mysql docker
do
    echo $i
done
[root@shell /server/scripts/day4]# sh for.sh
shell
mysql
docker

for循环命令
[root@shell /server/scripts/day4]# cat for.sh
#!/bin/sh
for i in $(seq 3)
do
    echo $i
done

[root@shell /server/scripts/day4]# cat for.sh 
#!/bin/sh
for i in `cat /etc/hosts`
do
    echo $i
done
[root@shell /server/scripts/day4]# sh for.sh 
127.0.0.1
localhost
localhost.localdomain
localhost4
4.2案例1: 从1加到100 面试题必备
[root@shell /server/scripts/day4]# cat for.sh 
#!/bin/sh
for i in `echo {1..100}`
do
      let count=$[$count+$i]
done
echo $count

seq -s + 100|bc
4.3批量获取10.0.0.0/24位的在线的主机 可以ping通则认为在线拼接
[root@shell /server/scripts/day4]# cat for.sh
#!/bin/sh
for i in  1 2 3
do
      ping -c1 -W1 10.0.0.$i &>/dev/null
      if [ $? -eq 0 ];then
       echo "10.0.0.$i 在线"
      fi
done
[root@shell /server/scripts/day4]# sh for.sh
10.0.0.1 在线
10.0.0.2 在线
4.4笔试题必备
[root@shell /server/scripts/day4]# cat for.sh 
#!/bin/sh
for i in  `seq 254`
do
     {
      IP=10.0.0.$i
      ping -c1 -W1 $IP &>/dev/null
      if [ $? -eq 0 ];then
       echo "$IP 在线"
      fi } &
    
done
wait
echo "在线取IP完成................"
修改网关 使用命令
好看默认路由
route -n
策略路由 ip rule  查看策略路由
静态路由 
ip route add 0/0 via 10.0.0.2
ip route del 0/0 via 10.0.0.2
使用命令配置一条网关为10.0.0.250
route add default gw 10.0.0.250
下一个接口地址是eth0
route add -net 192.168.3.0/24 dev eth0
子网划分(服务器量大)
4.5创建10个用户
[root@shell /server/scripts/day4]# cat user.sh 
#!/bin/sh
for i in `seq 10`
do
    user=oldboy$i
    id $user &>/dev/null
    if [ $? -eq 0 ];then
         echo "用户存在"
    else
        useradd  $user
        if [ $? -eq 0 ];then
        echo "$user 创建成功"
        fi
    fi
done

5.for循环结合case使用

[root@shell /server/scripts/day4]# cat user.sh 
#!/bin/sh
read -p "请输入用户名的前缀: " prefix
read -p "请输入用户的个数: " num
for i in `seq $num`
do
   echo ${prefix}$i
done
echo "1.删除"
echo "2.创建"
read -p "请选择删除以上用户或者是创建用户: " num1
if [ $num1 -eq 2 ];then
read -p "你确定要创建以上用户吗?[y|Y|yes|n|N|NO]: " re
case $re in
          y|Y|yes|YES)
    
          for a in `seq $num`
            do
                user=${prefix}$a
                id $user &>/dev/null
                if [ $? -eq 0 ];then
                 echo "用户存在"
                else
                useradd  $user
                if [ $? -eq 0 ];then
                echo "$user 创建成功"
                fi
                fi
          done
          ;;
      n|N|no|NO)
       echo "玩啥呢"
          ;;
          *)
       echo "USAGE $0 [y|Y|n|N]"
esac
elif [ $num1 -eq 1 ];then
              for b in `seq $num`
                do
                    user=${prefix}$b
                    id $user &>/dev/null
                    if [ $? -eq 0 ];then
                         userdel -r $user
                 if [  $? -eq 0 ];then
                 echo "$user 删除成功"
             fi
                    else
                echo "用户不存在"
                    fi
          done
fi

6.while循环

语法格式:
while 条件表达式 成立则执行 不成立 不执行  为真执行
do
    执行的命令 
done

语法格式2: 读入文件
for i in `cat file.txt`

while read line     #line 是自定义的变量 while是按照行读取文件 而不是for的空格
do 
   命令
done<file.txt
6.1笔试题 请写一个死循环脚本
[root@shell /server/scripts/day4]# cat while.sh 
#!/bin/sh
while true
do 
   sleep 2
    echo hehe
done

[root@shell /server/scripts/day4]# cat while.sh 
#!/bin/sh
i=0
while [ 10 -gt $i ]
do 
    echo hehe
    let i++
done
6.2while从1加到100
[root@shell /server/scripts/day4]# cat while.sh 
#!/bin/sh
i=1
while [ 100 -ge $i ]
do 
    count=$[count+i]
    let i++
done
echo $count
6.3读取文件
[root@shell /server/scripts/day4]# cat file.sh
#!/bin/sh
while read line
do
     echo $line
done</etc/hosts


[root@shell /server/scripts/day4]# cat file.sh
#!/bin/sh
while read line
do
     let i++
done</etc/passwd
echo $i 行

相关文章

  • shell脚本for循环

    1、循环表达式1 输出cl_acl2014cl_res2014cl_laptop2014cl_res2015cl_...

  • shell脚本循环

    1.case循环 1.2使用case写一个nginx的启动脚本 2.使用脚本写一个jumpserver跳板机 3....

  • shell

    1.双重循环 2.循环脚本 3.循环脚本 代码内容 文件内容 4.Linux shell中2>&1的含义解释 (全...

  • crontab指定时间执行程序(秒)

    crontab 指定秒执行原理: shell脚本for循环 for语句实现循环指定秒数执行。laravel框架 ...

  • 2.2.2 shell循环语句和文件读写

    shell脚本有3种可用的循环结构,他们分别是while循环,for循环,until循环。其中for循环用法比较特...

  • 【Linux 命令行与 shell 脚本编程大全】 13 更多的

    导览 在 shell 脚本中也可以使用 for 、 while 循环 until 循环则是和 while 循环在条...

  • Shell脚本

    1、脚本的执行 2、条件判断 3、循环执行(loop) 4、Shell脚本的追踪与调试

  • shell流程控制-for循环语句

    shell流程控制-for循环语句 for循环介绍 for语法 循环控制 脚本在执行任务的时候,总会遇到需要循环执...

  • linux命令

    1:shell脚本实现for循环:for[https://blog.csdn.net/taiyang1987912...

  • Shell脚本——条件判断和循环

    前言 条件判断和循环是shell脚本的基础,也决定了脚本运行流程,是写shell脚本十分重要的知识准备。本篇文章将...

网友评论

      本文标题:shell脚本循环

      本文链接:https://www.haomeiwen.com/subject/glekultx.html