目录
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