循环控制

for循环

1.列表for循环

在训后后边输入列表,可以是数字,或其他的列表等

  • 数字支持 {1…5},这种格式,起点 1,终点 5,步长 1;
  • 也可以指定步长{1…5…2} 起点 1,终点 5,步长 2;
  • 同时可以使用seq {seq 1 2 5} 起点 1,步长 2,终点 5;
  • 同时也支持读入命令,或正则表式。也可以直接输入一段内容,用分号隔开;
  • 支持读入启动脚本时,在后边跟的位置参数;
#脚本
[dps@ccod131 bak]$ cat for_canshu.sh 
#!/bin/bash

# 读取执行脚本时传进来的参数
position=0
echo "执行脚本传入的位置参数有 $@,共$#个"
for var in $@
do 
	((position++))
	echo "${position} is ${var}"
done

echo "position is ${position}"
------------------------------------------
#执行结果
[dps@ccod131 bak]$ ./for_canshu.sh a b c d
执行脚本传入的位置参数有 a b c d,共4个
1 is a
2 is b
3 is c
4 is d
position is 4

脚本

#!/bin/bash
echo "------------------------ 简单直接的列表"
#简单字符列表
for var in {a,b,v}
do
	echo "the var is ${var}"
done
echo "------------------------ 默认步长"
#数字,默认步长
for var in {1..3}
do
        echo "the var is ${var}"
done
echo "------------------------ 自定义步长"
#自定义步长
for var in {1..5..2}
do
        echo "the var is ${var}"
done
echo "------------------------ 使用seq"
#使用seq
for var in $(seq 6 3 9)
do
        echo "the var is ${var}"
done
echo "------------------------ 使用ls"
#使用ls读取当前目录的文件,和seq类似。均是命令
for var in $(ls)
do
        echo "the filename is ${var}"
done
echo "------------------------  使用*"
#使用*
for var in *
do
        echo "this is ${var}"
done

执行结果

------------------------ 简单直接的列表
the var is a
the var is b
the var is v
------------------------ 默认步长
the var is 1
the var is 2
the var is 3
------------------------ 自定义步长
the var is 1
the var is 3
the var is 5
------------------------ 使用seq
the var is 6
the var is 9
------------------------ 使用ls
the filename is awk.txt
the filename is for_list.sh
the filename is shift.sh
------------------------  使用*
this is awk.txt
this is for_list.sh
this is shift.sh

从上面的执行结果可以看出:

  1. “*”获取的是当前目录所有的文件,和“ls”起到了相同的效果
  2. seq和ls一样都是命令,这里使用$()引起来引用
不使用列表的for循环

可以删除in list,即使用缺省的in $@
示例:

#脚本
#不使用列表
position=0
echo "执行脚本传入的位置参数有 $@,共$#个"
for argument #使用缺省的in $@
do
        ((position++))
        echo "${position} is ${argument}"
done

echo "position is ${position}"
--------------------------------------------
#执行结果
执行脚本传入的位置参数有 a b c d,共4个
1 is a
2 is b
3 is c
4 is d
position is 4

从上述实例可以看出,使用了argument表示循环体,和上边的var一个效果

c语言风格的for循环(个人不喜欢)

该种类型的循环,循环判断条件由三部分组成:

  • 初始值,给循环变量赋予初始值;
  • 循环条件,当返回状态为非0时退出。即不满足条件时;
  • 循环变量的更改部分

注:当没有循环条件时,默认循环状态为0.故不天厨循环条件可构成死循环

#脚本
[dps@ccod131 bak]$ cat for_c.sh 
#!/bin/bash

for((i=0;i<=2;i++))
do
   echo "$i"
done
------------------------
#执行结果
[dps@ccod131 bak]$ ./for_c.sh 
0
1
2

while 循环

while循环的基本格式:

while [条件]
do
    执行体
done

while的循环条件返回状态为“0”时,继续循环。使用实例:

#脚本
[dps@ccod131 bak]$ sed '/^$/d' while.sh
#!/bin/bash
echo "假设我有N个苹果,你猜我有几个苹果。我给你提示高了或低了,最终猜出我手中的苹果数"
echo "please input your answer:"
read num
while [ "${num}" != 10 ]
do
  if [ "${num}" -lt 10 ]
  then
    echo "too small, try again!"
    read num
  else
    echo "too big, try again!"
    read num
  fi
done
echo "you are right!"

#执行效果 
[dps@ccod131 bak]$ ./while.sh 
假设我有N个苹果,你猜我有几个苹果。我给你提示高了或低了,最终猜出我手中的苹果数
please input your answer:
2
too small, try again!
11
too big, try again!
0
too small, try again!
-1
too small, try again!
10
you are right!

until循环

其使用方式while类似,不同的是,其条件语句返回码为“0”时,退出循环。和while刚好相反

until [条件]
do
    执行体
done

使用实例:

#脚本
#!/bin/bash
echo "假设我有N个苹果,你猜我有几个苹果。我给你提示高了或低了,最终猜出我手中的苹果数"
echo "please input your answer:"
read num
until [ "${num}" = 10 ]
do
  if [ "${num}" -lt 10 ]
  then
    echo "too small, try again!"
    read num
  else
    echo "too big, try again!"
    read num
  fi
done
echo "you are right!"
#执行效果
[dps@ccod131 bak]$ ./until.sh 
假设我有N个苹果,你猜我有几个苹果。我给你提示高了或低了,最终猜出我手中的苹果数
please input your answer:
1
too small, try again!
11
too big, try again!
10
you are right!

循环控制符

break,continue区别:

  • break跳出循环,指循环执行结束
  • continue跳出当前次的循环,会接着执行下一次循环
#脚本
#!/bin/bash

echo "break:只有1,2"
for var in {1..5}
do
  if [ ${var} -eq 3 ]
  then
    break
  fi
  echo "${var}"
done

echo "continue:显示1,2,4,5"
for var in {1..5}
do
  if [ ${var} -eq 3 ]
  then
    continue
  fi
  echo "${var}"
done

#执行结果
[dps@ccod131 bak]$ ./break_continue.sh 
break:只有1,2
1
2
continue:显示1,2,4,5
1
2
4
5

菜单选择–select

基本模式:

select name   [in   list ] 
do 
    statements that can use  $name... 
done

和for循环的使用类似,没有进行选择,直接回车会进入下一轮选择。可读取参数
操作实例:

#脚本
[dps@ccod131 bak]$ cat select_1.sh 
#!/bin/bash

echo "请输入您的姓名:"
read name

echo "${name}同学,下面会出3个选择,请进行相应的操作"
data=("男" "女")

echo "您的性别:"

select  var  in  ${data[@]}
do
  sex=${var}
  break
done

echo "最喜欢的水果:"
select var in "apple" "banana"
do
 fruit=${var}
 break
done

echo "幸运数字:"
select var in $@
do
 dat=${var}
 break
done

echo "${name}同学,您性别${sex},喜欢吃${fruit}.幸运数字 ${dat}!"

#执行效果
[dps@ccod131 bak]$ ./select_1.sh 3 6 9
请输入您的姓名:
阿娇
阿娇同学,下面会出3个选择,请进行相应的操作
您的性别:
1) 男
2) 女
#? 2
最喜欢的水果:
1) apple
2) banana
#? 2
幸运数字:
1) 3
2) 6
3) 9
#? 3
阿娇同学,您性别女,喜欢吃banana.幸运数字 9!

参考链接:

  1. https://blog.csdn.net/taiyang1987912/article/details/38929069
  2. https://blog.csdn.net/zaqwsx20/article/details/78647799
  3. https://blog.csdn.net/augusdi/article/details/40226243
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值