for i in 取值列表 # 取值列表以空格分隔进行取值 i为变量名称 数字、字符串、支持命令、序列
do命令集合
done#案例1.支持数字
[root@shell day04]# cat for.sh
#!/bin/bash
for i in 1 2 3
doecho $i
done
[root@shell day04]# sh for.sh
1
2
3#案例2.支持字符串
[root@shell day04]# cat for.sh
#!/bin/bash
for i in a b c d
doecho $i
done
[root@shell day04]# sh for.sh
a
b
c
d#案例3.支持序列
[root@shell day04]# cat for.sh
#!/bin/bash
for i in {a..d}
doecho $i
done
[root@shell day04]# sh for.sh
a
b
c
d[root@shell day04]# cat for.sh
#!/bin/bash
for i in {01..10}
doecho $i
done
[root@shell day04]# sh for.sh
01
02
03
04
05
06
07
08
09
10#案例4.支持命令
[root@shell day04]# cat for.sh
#!/bin/bash
for i in `ls`
doecho $i
done
[root@shell day04]# sh for.sh
case.sh
for.sh
jumpserver.sh
nginx.sh[root@shell day04]# cat 1.txt
aa
bb
cc
[root@shell day04]# cat for.sh
#!/bin/bash
for i in `cat 1.txt`
doecho $i
done
[root@shell day04]# sh for.sh
aa
bb
cc#案例5.可以利用循环次数做和值无关的事情
[root@shell day04]# cat for.sh
#!/bin/bash
for i in a b c
doecho hehe
done
[root@shell day04]# sh for.sh
hehe
hehe
hehe[root@shell day04]# cat for.sh
#!/bin/bash
for i in a b c
dolet a++
done
echo 循环了 $a 次
[root@shell day04]# sh for.sh
循环了 3 次#案例6.变量和字符串的拼接
[root@shell day04]# cat for.sh
#!/bin/bash
for i in a b c
doecho oldboy$i
done
[root@shell day04]# sh for.sh
oldboya
oldboyb
oldboyc[root@shell day04]# cat for.sh
#!/bin/bash
for i in 1 2 3
doecho oldboy$i
done
[root@shell day04]# sh for.sh
oldboy1
oldboy2
oldboy3#案例7.批量创建10个用户 oldboy01 -oldboy10
[root@shell day04]# cat for.sh
#!/bin/bash
for i in 1 2 3
douseradd oldboy$i
done
[root@shell day04]# sh for.sh
[root@shell day04]# tail -3 /etc/passwd
oldboy1:x:1001:1001::/home/oldboy1:/bin/bash
oldboy2:x:1002:1002::/home/oldboy2:/bin/bash
oldboy3:x:1003:1003::/home/oldboy3:/bin/bash[root@shell day04]# cat for.sh
#!/bin/bash
read -p "请输入用户名称的前缀: " prefix
read -p "请输入创建用户的个数: " num
for i in `seq $num`
douseradd $prefix$i
done[root@shell day04]# cat for.sh
#!/bin/bash
read -p "请输入用户名称的前缀: " prefix
#判断不能为空或者必须为字符串read -p "请输入创建用户的个数: " num
#判断必须是整数for i in `seq $num`
doecho $prefix$i
doneread -p "创建或者删除以上用户[y|d] " re
for i in `seq $num`
doif [ $re = y ];thenuser=$prefix$iid $user &>/dev/nullif [ $? -ne 0 ];then useradd $user && echo $user 创建成功elseecho $user已存在fielif [ $re = d ];thenuser=$prefix$iid $user &>/dev/null[ $? -eq 0 ] && userdel -r $user && echo $user 删除成功fi
done#案例7.创建不同的用户
[root@shell day04]# cat user.sh
#!/bin/bash
for i in `cat 1.txt`
douseradd $i
done
[root@shell day04]# cat 1.txt
oldboy
oldgirl
linux
[root@shell day04]# sh user.sh
[root@shell day04]# tail -3 /etc/passwd
oldboy:x:1006:1006::/home/oldboy:/bin/bash
oldgirl:x:1007:1007::/home/oldgirl:/bin/bash
linux:x:1008:1008::/home/linux:/bin/bash#在命令行执行for循环
for i in 取值列表;do 命令集合;done
[root@shell day04]# for i in {1..3};do echo $i;done
1
2
3#创建用户
[root@shell day04]# for i in {1..3};do useradd oldboy$i;done
[root@shell day04]# tail -3 /etc/passwd
oldboy1:x:1009:1009::/home/oldboy1:/bin/bash
oldboy2:x:1010:1010::/home/oldboy2:/bin/bash
oldboy3:x:1011:1011::/home/oldboy3:/bin/bash#删除用户
[root@shell day04]# for i in {1..3};do userdel -r oldboy$i;done#案例8.批量探测网络内10.0.0.0/24哪些主机在线 笔试题
10.0.0.1-10.0.0.254 哪些主机可以ping通
[root@shell day04]# cat ping.sh
#!/bin/bash
for i in `seq 254`
do{ip=10.0.0.$iping -c1 -W1 $ip &>/dev/null[ $? -eq 0 ] && echo $ip 在线} &
done
wait