shell脚本-for循环语句详解

举报
jiangxl 发表于 2022/04/14 00:23:17 2022/04/14
【摘要】 shell循环-for for循环默认跳过空行,遇到空行或者空格则不会去理睬 循环分为循环次数是固定的和循环次数不是固定的,固定的有for循环,不固定的有while和until循环 for循环每次都...

shell循环-for

for循环默认跳过空行,遇到空行或者空格则不会去理睬

循环分为循环次数是固定的和循环次数不是固定的,固定的有for循环,不固定的有while和until循环

for循环每次都以空格进行分隔,例如 lisi 123,本应该让i变量取一整行,结果却把lisi,123分别赋给了变量,达不到我们预期的效果,即使使用awk命令也同样只会输出lisi,123,因为$2不存在,详细请看036视频中的28分处,可以修改IFS分隔符值,让for处理文件时按回车分隔

IFS:内部字段分隔符

​ 例如修改IFS分隔符为回车,IFS=$’\n’或者IFS’'引号中间必须有回车,否侧会处理失败

break与continue的区别

​ break是跳出循环

​ continue是跳出本次循环

1.for语法结构

​ shell:

​ for 变量名 in 取值列表

​ do

​ 循环体

​ done

​ C语言风格:

​ for ((初值;条件;步长))

​ do

​ 循环体

​ done

for循环的特殊用法

#!/bin/bash
for i
do
	let $sum=$sum+$i
done

执行./for.sh 1 2
	输出3
for i后面不加东西相当于for i in $*也就是所有参数

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.1批量添加网卡接口

for i in {2..20}
do

        ifconfig ens33:$i 192.168.81.$i
done


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.2for实现批量主机ping探测

循环体中的{}&表示将循环体中的命令放到后台执行

wait命令:后台程序运行完后再执行后面的命令

#!/bin/bash
#-------------------实现批量主机ping探测--------------
#20200208
>ip.txt
for i in {2..254}
do
        {
        ip=192.168.81.$i
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo $ip | tee -a ip.txt
        fi
        }&
done
wait
echo "finish........."

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.3从文件中获取ip地址进行探测

for ip in  $(cat ip.txt)
do
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo "$ip is up..."
        else
                echo "$ip is down..."
        fi
done


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.4for实现批量用户创建

#!/bin/bash
#------------实现批量用户-------------------
#20200208
while :
do
        echo -en "\e[35m Please enter user prefix & user password & user numbers [zhangsan 123 10]: \e[0m" 
        read prefix password numbers
        printf "user infomation:
        -----------------------------------------------
        userprefix: $prefix
        userpassword: $password
        usernumbers: $numbers
        -----------------------------------------------
        "
        read -p "Aree you sure create user[y|n|q]: " action
        case $action in
        y|Y)
                break
                ;;
        q)
                exit
                ;;
        esac
done

for i in $(seq -w $numbers)
do
        user=$prefix$i
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                echo "user $user already exists..."
        else
                useradd $user
                echo "$password" |passwd --stdin $user &>/dev/null
                if id $user &>/dev/null;then
             			echo "user is created....."
                fi
        fi
done
                                                 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

1.5实现从文件中批量创建用户

#!/bin/bash
#------------------实现文件中批量创建用户-----------------
#20200209

if [ $# -eq 0 ];then
        echo "usage: $(basename $0) file"
        exit 1
fi

if [ ! -f $1 ];then
        echo "error file"
        exit 2
fi

IFS=$'\n'
for line in `cat $1`
do
        user=$(echo "$line" | awk '{print $1}')
        pass=$(echo "$line" | awk '{print $2}')
        id $line &>/dev/null
        if [ $? -eq 0 ];then
                echo "$i is already exists..."
        else
                useradd $user
                echo "$pass" |passwd --stdin $user &>/dev/null
                if [ $? -eq 0 ];then
                        echo "$i is created..."
                fi
        fi
done


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1.6批量创建用户改版

对比1.5主要有一下改进

​ 1.如果用户什么都不输入则重新走一遍循环

​ 2.如果用户输入的numbers不是整数则重新循环,并提示参数设置的不对

​ 3.如果用户只输入了用户前缀则也重新循环

#!/bin/bash
while :
do
        echo -en "\e[36m please enter userprefix & userpassword & usernumbers: \e[0m" 
        read userprefix userpassword usernumbers
        cat <<-EOF
        ----------------------------------------------------------------------------
                        user information
                        userprefix: $userprefix
                        userpassword: $userpassword
                        usernumbers: $usernumbers
        ----------------------------------------------------------------------------
        EOF
        if [[ ! "$usernumbers" =~ ^[0-9]+$ ]] && [ -z "$3" ];then
                echo -e "\e[31m error parameter\e[0m"
                echo -e "\e[33mPlease input again\e[0m"
                continue
        fi

        read -p "Are you sure user information[y|n|q]: " action
        case $action in
        y|Y)
                break
                ;;
        q)
                exit 1
        esac
done

for i in $(seq -w $usernumbers)
do
        user=$userprefix$i
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                echo -e "\e[31m user $user already exists... \e[0m"
        else
                useradd $user
                echo "$userpassword" | passwd --stdin $user &>/dev/null
                if [ $? -eq 0 ];then
                        echo -e "\e[32m $user is created....\e[0m"
                fi
        fi

done                                                 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

1.7根据模式进行批量用户创建和删除

1.让用户输入用户名前缀、密码、数量
2.$3必须是整数
3.将用户输入的信息打印成菜单
4.询问用户是否确认创建/删除
5.创建完成提示创建/删除成功
6.循环,用户输入的不正常则重新输入
7.如果创建则执行函数创建,删除则执行函数删除

#!/bin/bash
qrxx(){
        if [[ ! $usernumbers =~ ^[0-9]+$ ]];then
                echo -e "\e[31merror parameter\e[0m"
                continue
        fi

        cat <<-EOF
        +---------------------------------------------------------+
                        user information                         
                        userprefix: $userprefix                   
                        userpass: $userpass                       
                        usernumbers: $usernumbers                       
        +---------------------------------------------------------+
        EOF
}
create(){
        echo -en "\e[034mplease enter userprefix userpass usernumbers[ user 123 10 ]: \e[0m"
        read userprefix userpass usernumbers
        qrxx
        echo -en "\e[35mare you sure create user infomation [y|n] \e[0m"
        read action
        case $action in
        y|Y)
                for i in `seq -w $usernumbers`
                do
                        user=$userprefix$i
                        id $user &>/dev/null
                        if [ $? -ne 0 ];then
                                useradd $user
                                echo "$userpass" |passwd --stdin $user &>/dev/null
                                if [ $? -eq 0 ];then
                                        echo "$user is created..."
                                fi
                        else
                                echo "$user already exists..."
                        fi
                done
                ;;
        n|N)
                continue
                ;;
        *)
                echo "error action,please input [y|n]."
        esac
        #echo "Parameter is not complete"
}

delete(){
        echo -en "\e[034mplease enter userprefix  usernumbers[ user  10 ]: \e[0m"
        read userprefix  usernumbers
        qrxx
        echo -en "\e[35mare you sure delete user information [y|n]: \e[0m"
        read action
        case $action in
        y|Y)
                for i in $(seq -w $usernumbers)
                do
                        user=$userprefix$i
                        id $user &>/dev/null
                        if [ $? -eq 0 ];then
                                userdel -r $user
                                if [ $? -eq 0 ];then
                                        echo "$user is deleted..."
                                fi
                        else
                                echo "$user is not exists... "
                        fi
                done
                ;;
        n|N)
                continue
        esac
}

while :
do
        cat <<-EOF
        +---------------------------------------------------------+
        |                       MENU                              |
        |               1.Batch to create user                    |
        |               2.Batch delete user                       |
        |               q.exit                                    |     
        +---------------------------------------------------------+
        EOF
        echo -en "\e[33mPlease input you want to choose the mode: \e[0m" 
        read mode
        case $mode in
        1)
                create
                ;;
        2)
                delete
                ;;
        q)
                exit
                ;;
        *)
                echo "error"
        esac
done

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

文章来源: jiangxl.blog.csdn.net,作者:Jiangxl~,版权归原作者所有,如需转载,请联系作者。

原文链接:jiangxl.blog.csdn.net/article/details/106458268

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。