Shell脚本——for循环语句

1、for循环语法结构

1.1 列表循环

列表for循环:用于将一组命令执行已知的次数

  • 基本语法格式
for variable in {list}
	do
		command
		command
		...
	done
或者
for variable in a b c
	do
		command
		command
	done
  • 示例
#!/bin/bash
for i in {1..5}
 do
        echo hello world
 done


[root@localhost shell01]# chmod +x for1.sh
[root@localhost shell01]# ./for1.sh
hello world
hello world
hello world
hello world
hello world


或者

#!/bin/bash
for i in a b c d e
 do
        echo $i
 done
[root@localhost shell01]# ./for1.sh
a
b
c
d
e

1.2 不带列表循环

不带列表的for循环执行时由用户指定参数和参数的个数

  • 基本语法格式
for variable
	do
		command
		command
		...
	done
  • 示例
[root@localhost shell01]# vim for2.sh
[root@localhost shell01]# chmod +x for2.sh
[root@localhost shell01]# ./for2.sh
[root@localhost shell01]# cat for2.sh
#!/bin/bash
for i
 do
        echo hello
 done
[root@localhost shell01]# ./for2.sh a
hello
[root@localhost shell01]# ./for2.sh a 3
hello
hello
[root@localhost shell01]# bash -x for2.sh
[root@localhost shell01]# bash -x for2.sh a
 + for i in '"$@"'
 + echo hello
hello

1.3 类C风格的for循环

  • 基本语法结构
for(( expr1;expr2;expr3 ))
	do
		command
		command
		...
	done
	
for (( i=1;i<=5;i++ ))
	do
		echo $i
	done
	
expr1:定义变量并赋初始值
expr2:决定是否进行循环(条件)
expr3:决定循环变量如何改变,决定循环什么时候退出

2、循环控制语句

循环体: do…done之间的内容

  1. continue:继续;表示循环体内下面的代码不执行,重新开始下一次循环
  2. break: 打断;马上停止执行本次循环,执行循环体后面的代码
  3. exit:表示直接跳出程序

2.1 continue的使用

#!/bin/bash
for ((i=1;i<=5;i++))
do
        test $i -eq 3 && continue || touch /tmp/file$i
done
        echo "hello"
[root@localhost shell01]# vim for3.sh
[root@localhost shell01]# ./for3.sh
hello
[root@localhost tmp]# ll
总用量 1908
-rw-r--r--. 1 root root      0 53 16:59 file1
-rw-r--r--. 1 root root      0 53 16:59 file2
-rw-r--r--. 1 root root      0 53 16:59 file4
-rw-r--r--. 1 root root      0 53 16:59 file5

2.2 break的使用

#!/bin/bash
for ((i=1;i<=5;i++))
do
        test $i -eq 3 && break || touch /tmp/file$i
done
        echo "hello"
[root@localhost shell01]# chmod +x for3.sh
[root@localhost shell01]# ./for3.sh
hello
[root@localhost tmp]# ll
总用量 1908
-rw-r--r--. 1 root root      0 53 16:51 file1
-rw-r--r--. 1 root root      0 53 16:51 file2

2.3 exit的使用

#!/bin/bash
for ((i=1;i<=5;i++))
do
        test $i -eq 3 && exit || touch /tmp/file$i
done
        echo "hello"
[root@localhost shell01]# vim for3.sh
[root@localhost shell01]# ./for3.sh
hello
[root@localhost tmp]# ll
总用量 1908
-rw-r--r--. 1 root root      0 53 17:03 file1
-rw-r--r--. 1 root root      0 53 17:03 file2

3、应用案例

3.1 脚本计算1-100奇数和

  • 思路
  1. 定义一个变量来保存奇数的和 (sum=0)
  2. 找出1-100的奇数,保存到另一个变量里 (i=遍历出来的奇数
  3. 从1-100中找出奇数后,再相加,然后将和赋值给变量 (循环变量 for
  4. 遍历完毕后,将sum的值打印出来
#!/bin/bash
#计算1-100的奇数和
#定义变量来保存奇数和
sum=0

#for循环遍历1-100的奇数,并且相加,把结果重新赋值给sum

for i in {1..100..2}
do
        let sum=$sum+$i
done
#打印所有奇数的和
        echo "1-100的奇数和为:$sum"

[root@localhost shell01]# chmod +x sum.sh
[root@localhost shell01]# ./sum.sh
1-100的奇数和为:2500

3.2 判断所输整数是否为质数

质数(素数): 只能被1和它本身整除的数叫质数
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

  • 思路
  1. 让用户输入一个数,保存到一个变量里
  2. 如果能被其他数整除就不是质数
  3. 如果输入的数是1或者2取模根据上面判断又不符合,所以先排除1和2
[root@localhost shell01]# vim 4.sh
#!/bin/bash

read -p "请输入一个正整数:" number

# 先排除用户输入的数字1和2
if [ $number -eq 1 ]; then
    echo "$number不是质数"
    exit
elif [ $number -eq 2 ]; then
    echo "$number是质数"
    exit
fi

# 循环判断用户所输入的数字是否是质数
for (( i=2; i<=$((number/2)); i++ ))
do
        if [ $((number%i)) -eq 0 ]; then
            echo "$number不是质数"
            exit
        fi
done
echo "$number是质数"
[root@localhost shell01]# chmod +x 4.sh
[root@localhost shell01]# ./4.sh
请输入一个正整数:2
2是质数
[root@localhost shell01]# ./4.sh
请输入一个正整数:30
30不是质数
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值