循环语句for基本概述

循环语句for基本概述

01. for循环基础语法

for 变量名 in [ 取值列表 ]do 循环体done

img

02. for循环基本使用示例

#取值列表有多种取值方式,可以直接读取in后面的值,默认以空格做分割符

[root@qiudao /scripts]# cat for-1.sh
#!/bin/bash
for var in file1 file2 file3do    
echo "The text is $var"
done
[root@qiudao /scripts]# sh for-1.sh
The text is file1
The text is file2
The text is file3

03. for循环基本使用示例,列表中的复杂值,可以使用引号或转义字符""来加以约束

[root@qiudao /scripts]# cat for-2.sh
#!/bin/bash
for var in file1 "file2 hello" file3
do    
echo "The text is $var"
done
[root@qiudao /scripts]# sh for-2.sh
The text is file1
The text is file2 hello
The text is file3

#转义字符

[root@qiudao /scripts]# cat for-3.sh
#!/bin/bash
for var in file1 file \'2
do    
echo "The text is $var"
done
[root@qiudao /scripts]# sh for-3.sh
The text is file1
The text is file
The text is '2

04. for循环基本使用示例,从变量中取值

[root@qiudao /scripts]# cat for-4.sh
#!/bin/bash
list="file1 file2 file3"
for var in $list
do    
echo $var
done
[root@qiudao /scripts]# sh for-4.sh
file1
file2
file3

05. for循环基本使用示例,从命令中取值

[root@qiudao /scripts]# cat for-5.sh
#!/bin/bash
for var in `cat /etc/hosts`
do    
echo $var
done
[root@qiudao /scripts]# sh for-5.sh
127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6

06. for循环基本使用示例,自定义Shell分隔符。默认情况以空格为分隔符。通过IFS来自定义分隔符

#以冒号做分隔符                         IFS=:

#以冒号,分号和双引号作为字段分隔符          IFS=:;"

#以换行符作为字段分隔符                 IFS=$'\n'

#以回车为换行符
[root@qiudao /scripts]# cat for-6.sh
#!/bin/bash
IFS=$'\n'
for var in `cat /etc/hosts`
do
    echo $var
done
[root@qiudao /scripts]# sh for-6.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

#以:为分隔符
[root@qiudao /scripts]# cat for-7.sh
#!/bin/bash
IFS=:
list=`head -1 /etc/passwd`
for var in $list
do
    echo $var
done
[root@qiudao /scripts]# sh for-7.sh
root
x
0
0
root
/root
/bin/bash

07. for循环基本使用示例,C语言风格的for

#语法格式for ((i=0;i<10;i++))do commandsdone

#例1,单个变量,输出1到10之间的数字[root@qiudao /scripts]# cat for-8.sh#!/bin/bashfor ((i=0;i<10;i++))doecho num is $idone

[root@qiudao /scripts]# sh for-8.shnum is 0num is 1num is 2num is 3num is 4num is 5num is 6num is 7num is 8num is 9

例2,多个变量,同时输出1-9的升序和降序

解法一:

[root@qiudao /scripts]# cat for-9.sh

#!/bin/bash
for ((a=1,b=9;a<10;a++,b--))
do
echo num is $a $b
done

[root@qiudao /scripts]# sh for-9.sh
num is 1 9
num is 2 8
num is 3 7
num is 4 6
num is 5 5
num is 6 4
num is 7 3
num is 8 2
num is 9 1

#解法二:
[root@qiudao /scripts]# cat for-10.sh
#!/bin/bash
a=0
b=10
for i in {1..9}
do
let a++;
let b--;
echo num is $a $b
done
[root@qiudao /scripts]# sh for-10.sh
num is 1 9
num is 2 8
num is 3 7
num is 4 6
num is 5 5
num is 6 4
num is 7 3
num is 8 2
num is 9 1

2. 循环语句for场景示例

01. for循环场景示例一:通过读入文件中的用户,进行批量添加用户。

[root@qiudao /scripts]# cat for-11.sh
#!/usr/bin/bash

for i in $(cat user.txt)
do    
useradd $i &>/dev/null    
if [ $? -eq 0 ];then        
echo $i 用户创建成功    
else        
echo $i 用户已存在    
fi
done
[root@qiudao /scripts]# sh for-11.shtt1 用户创建成功tt2 用户创建成功tt3 用户创建成功tt4 用户创建成功tt5 用户创建成功tt6 用户创建成功tt7 用户创建成功tt8 用户创建成功tt9 用户创建成功tt10 用户创建成功

02. for循环场景示例二:通过读入文件中的用户:密码,进行批量添加用户。

[root@qiudao /scripts]# cat user.txt
user01:suibian
user02:suibian2
user03:suibian3
[root@qiudao /scripts]# cat for-12.sh
#!/usr/bin/bash
for i in $(cat user.txt)
do
    #1.取出来的行,使用awk进行分隔,然后分别赋值给user和pass两个变量
    user=$(echo $i|awk -F ":" '{print $1}')
    pass=$(echo $i|awk -F ":" '{print $2}')
    
    #2.判断用户是否存在
    id $user &>/dev/null
    
    #3.用户存在则提示已存在,否则添加用户,然后使用pass变量设定对应的密码
    if [ $? -eq 0 ];then
        echo "$user 已存在"
    else
        useradd $user
        echo "$pass" | passwd --stdin $user &>/dev/null
        echo "用户$user 创建成功!"
    fi
done
[root@qiudao /scripts]# sh for-12.sh
用户user1 创建成功!
用户user2 创建成功!
用户user3 创建成功!

03. for循环场景示例三:批量创建用户脚本,需要用户输入创建的用户数量,以及需要用户输入创建的前缀。

[root@qiudao /scripts]# cat for-13.sh
#!/usr/bin/bash
read -p "请输入你要创建的用户前缀: " user_qz
read -p "请输入你要创建的用户数量: " user_num
echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"
read -p "你确定要创建以上用户吗?[ y/n ] " readly
case $readly in
    y)
        for i in $(seq $user_num)
        do
            user=${user_qz}${i}
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                echo "useradd: user $user already exists"
            else
                useradd $user
                echo "useradd: user $user add successfully."
            fi
        done
        ;;
    n)
        echo "你想好了再创建......"
        exit
        ;;
    *)
        echo "请不要乱输入...."
        exit 
esac

04. for循环场景示例四:批量创建用户脚本,需要用户输入创建的用户数量(必须是整数),同时还需要用户输入前缀(前缀不能为空)。例如:前缀qls,个数10,代表创建qls1~qls10,总共10个用户。注意:此脚本仅root可执行,其他人无权限执行。

[root@qiudao /scripts]# cat for-14.sh#!/usr/bin/bashif [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then    echo "无权限执行......"    exitfi

read -p "请输入你要创建的用户前缀: " user_qzif [ -z $user_qz ];then    echo "请输入有效的值....."    exitfi

read -p "请输入你要创建的用户数量: " user_numif [[ ! $user_num =~ ^[0-9]+$ ]];then    echo "请输入整数"    exitfi

echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"read -p "你确定要创建以上用户吗?[ y/n ] " readly

case $readly in    y|yes|YES)        for i in $(seq $user_num)        do            user=${user_qz}${i}            id $user &>/dev/null            if [ $? -eq 0 ];then                echo "useradd: user $user already exists"            else                useradd $user                echo "useradd: user $user add successfully."            fi        done

​        ;;    n|no|NO)        echo "你想好了再创建......"        exit        ;;    *)        echo "请不要乱输!"        exitesac

05. for循环场景示例五:批量创建用户脚本,需要用户输入创建的用户数量(必须是整数),同时还需要用户输入前缀(前缀不能为空)。例如:前缀qls,个数10,代表创建qls1~qls10,总共10个用户。注意:此脚本仅root可执行,其他人无权限执行。用户的密码使用随机密码,并保存到某一个指定的文件中。

[root@backup scripts]# cat 3.sh 
#!/bin/bash
if [ ! $USER == "root" ] && [ ! $UID -eq 0 ];then
    echo "该用户没有执行权限"
    exit
fi
read -p "请输入你想要的前缀:" dir
if [ -z $dir ];then
    echo " 输入不能为空,不支持"
    exit
fi
if [[ ! $dir =~ ^[a-Z]+$ ]];then
        echo "输入错误,请输入字母:"
        exit
    
fi
read -p "请输入创建用户的数量:" num
if [ -z $num ];then
    echo "不能为空"
    exit
else
    if [[ ! $num =~ ^[0-9]+$ ]];then
        echo "请输入数字为整数"
        exit
    fi
fi
read -p "你创建的用户为${dir}1..${dir}n${num},确认创建[y|n]: " rc
case $rc in
    y|Y|yes)
        for i in $(seq $num)
        do
            user=${dir}${num}
            pass=$(echo $((RANDOM)) |md5sum |cut -c 2-24)
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                echo "用户已经存在"
            else
                useradd $user &>/dev/null && echo $pass |passwd --stdin $user &>/dev/null
                if [ $? -eq 0 ];then
                    echo "用户$user 创建成功,密码ok"
                    echo "用户:$user 密码:$pass" >>/tmp/q.txt
                    echo "用户密码文件在/tmp/q.txt"
                else
                    echo "用户创建失败"
                    exit
                fi
            fi
        done
        ;;
    n|N|no)
        echo "你不想创建,推迟"
        exit
        ;;
    *)
        echo "请输入正确的格式[y|n]"
        exit
esac

06. for循环场景示例六:批量删除(用户需要输入用户前缀及数字),有就删除,没有就提示没有该用户。

[root@backup scripts]# cat 4.sh 
#!/bin/bash
if [ ! $USER == "root" ] && [ ! $UID -eq 0 ];then
    echo "该用户没有执行权限"
    exit
fi
read -p "请输入你想要删除的前缀:" dir
if [ -z $dir ];then
    echo " 输入不能为空,不支持"
    exit
fi
if [[ ! $dir =~ ^[a-Z]+$ ]];then
        echo "输入错误,请输入字母:"
        exit
    
fi
read -p "请输入想要删除用户的数量:" num
if [ -z $num ];then
    echo "不能为空"
    exit
else
    if [[ ! $num =~ ^[0-9]+$ ]];then
        echo "请输入数字为整数"
        exit
    fi
fi
read -p "你删除的用户为${dir}${num},确认删除[y|n]: " rc
case $rc in
    y|Y|yes)
        for i in ${dir}${num}
        do
            user=${dir}${num}
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                userdel $user &>/dev/null
                if [ $? -eq 0 ];then
                    echo "用户$user已删除"
                else
                    echo "用户$user删除失败"
                fi
            else
                echo "用户不存在"
                exit
            fi
        done
        ;;
    n|N|no)
        echo "你不想删除,退出"
        exit
        ;;
    *)
        echo "请输入正确的格式[y|n]"
        exit
esac

07. for循环场景示例七:批量探测主机存活状态及22端口状态

1)需要用循环,循环254次2)探测主机使用ping命令

[root@backup scripts]# cat 5.sh 
#!/bin/bash

for i in {30..50}
do
    ip=172.16.1.$i
    ping -c 1 -w 1 $ip &>/dev/null
    if [ $? -eq 0 ];then
        echo "$ip是接通的"
    else
        echo "$ip是不接通的"
    fi
done

08. for循环场景示例八:编写一个上课随机点名脚本。

1.需要有名字2.需要循环的打印这些名单3.随机挑选一个数字进行打印

[root@backup scripts]# cat student.sh 
#!/bin/bash

stu_num=`wc -l student.txt|awk '{print $1}'`
for i in $(seq $stu_num)
do
    num=$(echo $(( $RANDOM % ${stu_num} +1 )))
    sed -n "${num}p" student.txt
    sleep 0.2
done
stu_name=$(sed -n "${num}p" student.txt)
echo -e "天选之子: \033[32m ${stu_name}....\033[0m"

09. for循环场景示例九:使用for循环实现数据库的分库分表备份。

[root@backup scripts]# cat mysql.sh
#!/bin/bash
##############################################################
# File Name: mysql.sh
# Time: 2019-10-22-15:23:08
# Author: qls
# Organization: www.increase93.com
##############################################################
db_user=root

db_name=`mysql -uroot -e "show databases;"|sed 1d|grep -v .*_schema`
date=`date +%F`
for database in $db_name
do
    if [ ! -d /backup/$database ];then
        mkdir -p /backup/$database
    fi
    mysqldump -u$db_user -B $database >/backup/$database/${database}_${date}.sql &>/dev/null
    if [ $? -eq 0 ];then
        echo "$database备份成功"
        db_table=$(mysql -u$db_user -e "use $database;show tables;"|sed 1d)
        for tables in $db_table
        do
            mysqldump -u$db_user  $database $tables >/backup/$database/${database}_${tables}_${date}.sql 
            if [ $? -eq 0 ];then
                echo "$db_table表备份成功"
            else
                echo "$db_table表备份失败"
                continue
            fi
        done
    else
        echo "数据库$database备份失败"
        continue
    fi
done
1.怎么备份数据库    mysqldump -uroot -p123.com --single-transaction -B world > world_database.sql2.怎么备份数据库的表mysqldump -uroot -p123.com --single-transaction world city > world_city_tables.sql3.备份到哪儿/mysql_dump/oldboy/city_2019_07_16.sql

#!/usr/bin/bash
db_name=$(mysql -uroot -p123.com -e "show databases;"|sed 1d |grep -v ".*_schema")
DB_User=root
DB_Pass=123.com
Date=$(date +%F)

for database_name in $db_name
do
    #1.准备每个库的目录
    DB_Path=/mysql_dump/$database_name
    if [ ! -d $DB_Path ];then
        mkdir -p $DB_Path
    fi
    #2.备份数据库
    mysqldump -u$DB_User -p$DB_Pass --single-transaction 
    -B $database_name > $DB_Path/${database_name}_${Date}.sql
    echo -e "\033[31m $database_name 数据库
已经备份完成..... \033[0m"
    
    #3.备份数据库的每一个表
    tb_name=$(mysql -u$DB_User -p$DB_Pass -e "use ${database_name};show tables;"|sed 1d)
    
    #4.批量的备份数据库的表
    for table_name in $tb_name
    do
        #5.备份数据库的表数据
        mysqldump -u$DB_User -p$DB_Pass --si

10. for循环场景示例十:用于判断mysql数据库主从的脚本,需要邮件通知。

1.判断io线程和sql线程是否都为yes,如果是则成功。2.如果io线程失败,则直接邮件通知,如果sql线程失败,则检查是什么错误状态码,根据状态码修复。3.无法修复,或任何错误代码太复杂建议,直接发邮件通知管理员。

[root@qiudao /scripts]# cat for-20.sh
#!/usr/bin/bash
IO_Status=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Slave_IO_Running/ {print $2}')
SQL_Status=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Slave_SQL_Running/ {print $2}')

slave_sql_error_message(){   
mysql -uroot -p123.com -e "show slave status\G"|grep "Last_SQL" > /tmp/sql_err.log   
mail -s "MySQL Master SLave SQL Error $(date +%F)" [email protected] < /tmp/sql_err.log    
echo "邮件通知成功......"
}

slave_io_error_message(){    
mysql -uroot -p123.com -e "show slave status\G"|grep "Last_IO" > /tmp/io_err.log    
mail -s "MySQL Master SLave IO Error $(date +%F)" [email protected] < /tmp/io_err.log    
echo "邮件通知成功......"}

if [ $IO_Status == "Yes" ] && [ $SQL_Status == "Yes" ];then   
echo "MySQL主从正常"
else    
#1.判断IO异常    
if [ ! $IO_Status == "Yes" ];then        slave_io_error_message        
exit    
fi
#2.判断SQL异常    
if [ ! $SQL_Status == "Yes" ];then       
SQL_Err=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Last_SQL_Errno/ {print $2}')       
#3.精细化判断主从不同步的问题        
case $SQL_Err in            
1007)                
echo "主从的SQL出现问题,尝试使用set global sql_slave_skip_counter=1; 跳过错误"                
sleep 2                
mysql -uroot -p123.com -e "stop slave;set global sql_slave_skip_counter=1;start slave;"                SQL_Err_1=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Last_SQL_Errno/ {print $2}')                if [ $SQL_Err_1 -eq 0 ];then                    
echo "尝试跳过了一次,恢复MySQL数据库成功"                else                    
slave_sql_error_message                
fi                    
;;            
1032)                    
slave_sql_error_message                    
;;            
*)                    
slav

3. 循环语句while基本概述

while循环语句,只要条件成立就反复执行对应的命令操作,直到命令不成立或为假

01. while循环基础语法

#当条件测试成立(条件测试为真),执行循环体

while 条件测试
do 循环体
done

img

02. while循环基本使用示例,降序输出10到1的数字

[root@qiudao /scripts]# cat while-1.sh
#!/bin/bash
var=10
while [ $var -gt 0 ]
do    
echo $var    
var=$[$var-1]done

#简单加法表
[root@qiudao /scripts]# cat while-2.sh
#!/usr/bin/bash

a=1
b=10
while [ $a -le 10 ]
do    
sum=$(( $a + $b ))   
echo $a + $b = $sum    
let a++    
let b--
done

03. while循环基本使用示例,输出如下图,两数相乘。

#自增[root@qiudao /scripts]# cat while-3.sh
#!/bin/bash
num=9
while [ $num -ge 1 ]
do    
sum=$(( $num * $num ))    
echo "$num * $num = $sum"   
num=$[$num-1]
done

\#自减[root@qiudao /scripts]# cat while-4.sh
#!/bin/bash
num=1
while [ $num -le 9 ]
do    
sum=$(( $num * $num ))    
echo "$num * $num = $sum"    
num=$[$num+1]
done

4. 循环语句while场景示例

01. 使用while读入文件方式,批量创建用户,while默认按行取值

[root@qiudao /scripts]# cat while-5.sh
#!/usr/bin/bash
while read line
do
    #1.判断用户是否存在
    id $line &>/dev/null
    if [ $? -eq 0 ];then
        echo "User: $line already exists"
    else
        useradd $line &>/dev/null
        echo "User: $line Create Ok"
    fi
done<user.txt
[root@qiudao /scripts]# cat user.txt
ttt1
ttt2
ttt3
ttt4
ttt5

**02. 

02. 使用while读入文件方式,批量创建用户以及密码[user:passwd]

使用while读入文件方式,批量创建用户,并赋予一个随机密码**

[root@backup scripts]# cat us.sh 
#!/bin/bash

while read line
do
    user=`echo $line|awk -F: '{print $1}'`
    pass=`echo $line|awk -F: '{print $2}'`
    id $user &>/dev/null
    if [ $? -eq 0 ];then
        echo "$user已经存在"
    else
        useradd $line &>/dev/null && echo $pass |passwd --stdin $user &>/dev/null
        if [ $? -eq 0 ];then
            echo "ok"
        else
            echo "faile"
        fi
    fi
done < user.txt

04. while循环场景示例:完成如下猜数字游戏

[root@backup scripts]# cat cai.sh 
#!/bin/bash
##############################################################
# File Name: cai.sh
# Time: 2019-10-22-16:51:15
# Author: qls
# Organization: www.increase93.com
##############################################################
sum=`echo $(( RANDOM % 100 +1 ))`
echo "请输入你猜的整数"
i=0
while true
do
    read -p "请输入你猜的数字:" int
    if [[ ! $int =~ ^[0-9]+$ ]];then
        echo "必须输入整数"
        continue
    fi
    if [ -z $int ];then
        echo "不能为空"
        continue
    fi
    if [ $int -gt 100 -o $int -lt 0 ];then
        echo "100以内的数字"
        continue
    fi
    if [ $int -gt $sum ];then
        echo "猜大了"
    elif [ $int -lt $sum ];then
        echo "猜小了"
    else
        echo "ok"
        break
    fi
    let a++
done
echo "总次数 $(( $a +1 ))"

景示例:随机点菜脚本**

[root@backup scripts]# cat tayn.sh
#!/bin/bash
main(){
cat <<EOF
#####################################
1.糖醋排骨   30¥
2.清蒸鲈鱼   50¥
3.青椒炒蛋   20¥
4.烤羊排     199¥
5.结束点菜
######################################
EOF
}
>caidan.txt
echo "您好!欢迎进入点菜系统"
while true
do
    main
    read -p "请开始点菜:" dian
    case $dian in
        1)
            echo "糖醋排骨:价格30元"
            echo "糖醋排骨:30" >>caidan.txt
            ;;
        2)
            echo "清蒸鲈鱼:价格50元"
            echo "清蒸鲈鱼:50" >>caidan.txt
            ;;
        3)
            echo "青椒炒蛋:价格20元"
            echo "青椒炒蛋:20" >>caidan.txt
            ;;
        4)
            echo "烤羊排:价格199元"
            echo "烤羊排:199" >>caidan.txt
            ;;
        5)
            echo "结束点菜,菜品和价格如下"
            sort caidan.txt |uniq -c |sort -rn |awk '{print "您点了 "$2"总共"$1""}'
            awk -F: '{i+=$2}END{print "总价格:"i"元"}' caidan.txt
            exit
            ;;
        *)
            echo "本店无您的需求,抱歉"
            continue
    esac
done

5. 内置跳出循环语句指令

在我们使用循环语句进行循环的过程中,有时候需要在未达到循环结束条件时强制跳出循环,那么Shell给我们提供了内置方法来实现该功能:exit、break、continue

01. exit,退出整个程序

[root@qiudao /scripts]# cat for-exit.sh

#!/bin/bash
for i in {1..3}
do
echo "123"
exit
echo "456"
done
echo "done ......"

\#执行结果[root@qiudao /scripts]# sh for-exit.sh
123

02. break,结束当前循环,或跳出本层循环

[root@qiudao /scripts]# cat for-break.sh
#!/bin/bash
for i in {1..3}
do
echo "123"
break
echo "456"
done
echo "done ......"

\#执行结果[root@qiudao /scripts]# sh for-break.sh
123
done ......

img

03. continue,忽略本次循环剩余的代码,直接进行下一次循环。

[root@qiudao /scripts]# cat for-continue.sh
#!/bin/bash
for i in {1..3}do
echo "123"
continue
echo "456"
done
echo "done ......"

\#执行结果[root@qiudao /scripts]# sh for-continue.sh
123
123
123
done ......

img

04. 习题:先扫描内网网段所有主机,存活的则下发当前主机的公钥。

[root@qiudao /scripts]# cat key.sh
#!/bin/bash

删除旧的密钥对

rm -f ~/.ssh/id_rsa*

创建密钥对

ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" &>/dev/null

批量分发公钥的操作

for ip in {2..10}
do
ping -W1 -c1 172.16.1.$ip &>/dev/null
if [ $? -eq 0 ];then
     #z注意密码根端口
     sshpass -p123 ssh-copy-id -p2222 -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip &>/dev/null
     if [ $? -eq 0 ];then
        echo -e "\033[32mhost 172.16.1.$ip Distribution of the secret key Success!!! \033[0m"
        echo
    else
        echo -e "\033[31mhost 172.16.1.$ip Distribution of the secret key Failed !!! \033[0m"
        echo
        fi
else
    echo -e "\033[31mhost 172.16.1.$ip Destination Host Unreachable! \033[0m"
    continue
fi
done

猜你喜欢

转载自www.cnblogs.com/1naonao/p/11722465.html