shell流程控制之循环,了解for、while循环语句,轻松编写九九乘法表

本文详细解析了shell编程中的for循环(带列表、不带列表及C风格)、while循环的用法,涵盖乘法表、用户创建与验证、IP检测、参数列表操作和文件读取等实例,帮助理解循环结构在实际任务中的灵活运用。
摘要由CSDN通过智能技术生成

目录

题目

一、步进循环语句for

1、带列表的for循环语句

2、 不带列表的for循环语句

 3、类C风格的for循环语句

二、while循环语句 


题目

1、9 * 9 乘法表,for列表循环,for循环(c语言风格), while循环,可选单层循环
2、使用for循环创建30个用户: test01~test30, 并设置密码为test01123456~test30123456
3、使用循环去判断网段内的IP(1~254),本机除外,可以ping通的使用 ssh远程登录
4、使用$@和$*作为for循环后的列表,并体现出区别
5、使用循环去读取文件内容并输出: 3中方式(1.exec+while循环 2.管道符+while循环 3.重定向+while)

一、步进循环语句for

        for循环是最简单,也是最常用的循环语句。与其他的程序设计语言一样, for 循环都是初学者在学习循环结构时的入门课程。for 循环通常用于遍历整个对象或者数字列表。按照循环条件的不同, for 循环语句可以分为带列表的for 循环、不带列表的 for 循环以及类 C 风格的 for 循环。

1、带列表的for循环语句

for variable in list 
do 
    statement1 
    statement2 
    ... 
done
        在上面的语法中,variable 称为循环变量, list 是一个列表,可以是一系列的数字或者字符串,元素之间使用空格隔开。do done 之间的所有的语句称为循环体,即循环结构中重复执行的语句。
        for 循环体的执行次数与list 中元素的个数有关。在带列表的 for 语句执行时, Shell 会将 in 关键字后面的 list 列表的第 1 个元素的值赋给变量variable ,然后执行循环体;当循环体中的语句执行完毕之后, Shell 会将列表中的第2 个元素的值赋给变量 variable ,然后再次执行循环体。当 list 列表中的所有的元素都被访问后, for 循环结构终止,程序将继续执行done 语句后面的其他的语句 

2、 不带列表的for循环语句

        在某些特殊情况下,for 循环的条件列表可以完全省略,称为不带列表的 for 循环语句。如果没有为 for 循环提供条件列表,Shell 将从命令行获取条件列表。
for variable 
do 
    statement1 
    statement2 
    ... 
done

 3、C风格的for循环语句

for ((expression1;expression2;expression3)) 
do 
    statement1; 
    statement2; 
    ... 
done
        在上面的语法中,for 循环语句的执行条件被 2 个圆括号包括起来。执行条件分为 3 个部分,由 2 个分号隔开,第1 部分 expression1 通常是条件变量初始化的语句;第 2 部分 expression2 是决定是否执行 for 循环的条件。当expression2 的值为 0 时,执行整个循环体;当 expression2 的值为非 0 时,退出 for 循环体。第3 部分,即表达式 expression3 通常用来改变条件变量的值,例如递增或者递减等。

二、while循环语句 

        while循环是另外一种常见的循环结构。使用 while 循环结构,可以使得用户重复执行一系列的操作,直到某个条件的发生。这听起来好像跟until 循环非常相似,但是与 until 语句相比, while 语句有着较大的区别。
while expression 
do 
    statement1 
    statement2 
    ... 
done
        在上面的语法中,expression 表示 while 循环体执行时需要满足的条件。虽然可以使用任意合法的 Shell命令,但是,通常情况下,expression 代表一个测试表达式,当 expression 的值为 0 时才执行循环体中的语句,每次执行到done 时就会重新判断 while 条件表达式是否成立,当 expression 的值为非 0 值时,将退出循环体。与其他的循环结构一样,do done 2 个关键字之间的语句构成了循环体。
1、9 * 9 乘法表,for列表循环,for循环(c语言风格), while循环,可选单层循环
方法一:
[root@localhost day4]# vim task_1_1.sh
#!/bin/bash
for i in {1..9}
do
    echo `eval "echo -e {1..$i}*$i'\n'|bc -i -q"`
done

[root@localhost day4]# bash task_1_1.sh 
1*1 1
1*2 2 2*2 4
1*3 3 2*3 6 3*3 9
1*4 4 2*4 8 3*4 12 4*4 16
1*5 5 2*5 10 3*5 15 4*5 20 5*5 25
1*6 6 2*6 12 3*6 18 4*6 24 5*6 30 6*6 36
1*7 7 2*7 14 3*7 21 4*7 28 5*7 35 6*7 42 7*7 49
1*8 8 2*8 16 3*8 24 4*8 32 5*8 40 6*8 48 7*8 56 8*8 64
1*9 9 2*9 18 3*9 27 4*9 36 5*9 45 6*9 54 7*9 63 8*9 72 9*9 81
方法二:
[root@localhost day4]# vim task_1_2.sh
#!/bin/bash
for ((i=1;i<10;i++))
do
    for ((j=1;j<=i;j++))
    do
        echo -n $i*$j="$(($i*$j)) "
    done
    echo  
done

[root@localhost day4]# bash task_1_2.sh 
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
方法三:
[root@localhost day4]# vim task_1_3.sh
#!/bin/bash
i=1
j=1
while (($i<10))
do
        while (($j<$i+1))
        do
                echo -n "$i*$j=$(($i*$j)) "
                let j++
        done
        let i++
        j=1
        echo -e
done

[root@localhost day4]# bash task_1_3.sh 
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

2、使用for循环创建30个用户: test01~test30, 并设置密码为test01123456~test30123456 

[root@localhost day4]# vim task_2.sh
#!/bin/bash
for user in user{01..30}
do
  if ! id -u $user &> /dev/null
    echo $user
    echo "$user"123456 | passwd --stdin $user
  else
    echo "$user is exits"
  fi
done

[root@localhost day4]# bash task_2.sh
#再执行一次,用户是否成功添加
[root@localhost day4]# bash task_2.sh
user01 is exits
user02 is exits
user03 is exits
user04 is exits
user05 is exits
user06 is exits
user07 is exits
user08 is exits
user09 is exits
user10 is exits
user11 is exits
user12 is exits
user13 is exits
...

3、使用循环去判断网段内的IP(1~254),本机除外,可以ping通的使用 ssh远程登录

[root@localhost day4]# vim task_3.sh
#!/bin/bash
for ip in 192.168.80.{1..254}
do
    if ping -c 2 $ip &> /dev/null ;then
        echo "$ip is up .."
    else
        echo "$ip is down .."
    fi
done

[root@localhost day4]# bash task_3.sh 
192.168.80.1 is up ..
192.168.80.2 is up ..
192.168.80.3 is down ..
192.168.80.4 is down ..
192.168.80.5 is down ..
192.168.80.6 is down ..
192.168.80.7 is down ..
192.168.80.8 is down ..
192.168.80.9 is down ..
...

4、使用$@和$*作为for循环后的列表,并体现出区别 

[root@localhost day4]# vim task_4.sh
#!/bin/bash
for i in "$@"
do      
    echo $i
done

echo "*****************"
for j in "$*"
do
    echo $j
done

[root@localhost day4]# bash task_4.sh a b c d
a
b
c
d
*****************
a b c d

5、使用循环去读取文件内容并输出: 3中方式(1.exec+while循环 2.管道符+while循环 3.重定向+while)

[root@localhost day4]# cat file1.txt 
www.1.com
www.2.com
www.3.com
www.4.com
方法一:采用exc读取文件,然后进入while循环处理
[root@localhost day4]# vim task_5_1.sh 
#!/bin/bash
exec < file1.txt
while read file
do
    echo $file
done
[root@localhost day4]# bash task_5_1.sh 
www.1.com
www.2.com
www.3.com
www.4.com

方法二:使用cat读文件,然后通过管道进入while循环处理
[root@localhost day4]# vim task_5_2.sh 
#!/bin/bash
cat file1.txt | while read file
do
    echo $file
done
[root@localhost day4]# bash task_5_2.sh 
www.1.com
www.2.com
www.3.com
www.4.com

方法三:通过在while循环结尾,使用输入重定向方式
[root@localhost day4]# vim task_5_3.sh 
#!/bin/bash
while read file
do
    echo $file
done < file1.txt
[root@localhost day4]# bash task_5_3.sh 
www.1.com
www.2.com
www.3.com
www.4.com

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值