Linux Shell——循环与结构化命令

for循环

for variable in (list)
do
	command
done	
#!/bin/bash

for variable in {1..5}
do
	echo "Hello,Welcome $variable times"
done
#!/bin/bash
#步长为2
for variable in {1..5..2}
do
	echo "Hello,Welcome $variable times"
done
不带列表的for循环
for variable
do
	command
	command
done
类C风格的for循环
for((expr1;expr2;expr3))
do
	command
	command
done	

while循环

while expression
do
	command
	command
done
计数器控制的while循环
counter=1
while expression
do
	command

done
结束标记控制的while循环
read variable
while [[ "$variable" != sentinel ]]
do
	read variable
done
标志控制的while循环
signal=0

while (( signal != 1 ))
do
	if expression
	then
		signal=1
	fi
done
命令行控制的while循环
while [[ "$*" != "" ]]
do
	echo "$1"
	shift
done

until循环

在执行while循环时,只要expression的退出状态为0,将一直执行循环体,until命令和while命令类似,但区别是util循环中的expression的退出状态不为0,循环体将一直执行下去,直到退出状态为0。

循环控制符

break
continue

select结构

select variable in {list}
do
	command
	break
done
#!/bin/bash

echo "What is your favorite color?"

select color in "red" "blue" "green"
do
	break
done


echo "You have selected $color"

 ./select_exam1.sh 
What is your favorite color?
1) red
2) blue
3) green
#? 1
You have selected red


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值