Shell的循环语句

目录

FOR循环

for循环带列表

seq

for循环不带列表

类c的for循环

while循环

until循环

select循环


FOR循环

for循环带列表

语法

for variable in list 
do 
statement1 
statement2 
... 
done 

这个in吧  很有意思  可以跟着的东西很多

直接跟元素

for IP in 192.168.132.128 192.168.132.129

接括号

for IP in 192.168.132.12{8..9}

接seq

for IP in $(seq -f "192.168.132.12%1g" 1 5)

seq

这里提一下seq的用法——seq用于输出序列化的字符数字等

[root@localhost fuxi]# seq 1 5
1
2
3
4
5
[root@localhost fuxi]# seq 1 2 10
1
3
5
7
9

选项:

-s 指定输出的分隔符,默认为\n,即默认为回车换行

[root@localhost fuxi]# seq -s + 1 10
1+2+3+4+5+6+7+8+9+10

-w 指定为定宽输出,不能和-f一起用

[root@localhost fuxi]# seq -w 99 105
099
100
101
102
103
104
105

-f 按照指定的格式输出,不能和-w一起使用,在没有使用-f选项指定格式时,默认格式为%g

[root@localhost fuxi]# seq -f '%03g' 8 11
008
009
010
011
[root@localhost fuxi]# seq -f "dir%g" 1 5
dir1
dir2
dir3
dir4
dir5

for循环不带列表

#如果没有为for循环提供条件列表,Shell将从命令行获取条件列表

for variable 
do 
statement1 
statement2 
... 
done 


#由于系统变量$@同样可以获取所有的参数,所以以上的语法等价于以下语法:

for variable in $@或$* 
do 
statement1 
statement2 
... 
done 

例如

#!/bin/bash 
for arg 
do 
echo $arg 
done 

 

类c的for循环

这个大家就熟悉了
for ((expression1;expression2;expression3)) 
do 
statement1; 
statement2; 
... 
done

sum=0
for(i:=1;i<=100;i++)
do
    sum=$[sum+i]
done

echo "$sum"

while循环

语法

while 条件
do
	内容
done

until循环

until expression 
do 
statement1 
statement2 
... 
done 

在上面的语法中,expression是一个条件表达式。当该表达式的值不为0时,将执行do和

done之间的语句;当expression的值为0时,将退出until循环结构,继续执行done语句后面

的其它的语句。

i=1 
until [ $i -gt 20 ]
do 
if [ $i -le 9 ] 
then 
user=test0$i 
else 
user=test$i 
fi 
if id -u $user &> /dev/null 
then 
userdel -r $user 
else 
echo "$user is not exists..." 
fi 
let i++ 
done

select循环

select 变量名 [ in 菜单值列表 ] 
do 
statement1 
statement2 
... 
done 

例如

#!/bin/bash 
select mysql_version in 5.1 5.6 
do 
echo $mysql_version 
done 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值