1.什么是循环:
重复去完成⼀个任务。循环在shell脚本中有什么使⽤的场景:
⽐如:每隔5分钟执⾏⼀次ping操作,可以使⽤定时任务,我们还可以使⽤循环的⽅式。
⽐如:每隔5分钟执⾏⼀次ping操作,ping的操作只执⾏100次,循环⽅式来实现。
2.什么是for循环:
for循环也叫条件循环,假设给5个条件,那么我的for循环就会循环5次。结束后,脚本退出。
3.for循环基础语法示例
语法:
for 变量名 in [ 取值列表 ]
do
循环体需要执⾏的内容
done
语法一:
for var in test1 test2 test3
do
echo 123 #请问123会输出⼏次 3次(给予可三个条件)
echo This is $var #结果是啥
done
语法二:
给了4个条件还是6个条件?
for var in file1 "file2 file3" file4 "hello world"
do
echo the text is $var
done
语法三:
为list变量赋予了三个值
list="file1 file2 file3"
# 循环list变量,将list变量中的值挨个复制给 i 变量
for i in $list
do
echo var is $i
done
语法四:
for i in $(cat /etc/hosts)
do
echo "$i"
done
语法五:
for ((i=0;i<10;i++))
do
commmands
done
#第⼀步先执⾏ i=0
#第⼆部执⾏ 0<10
#第三部执⾏ commands命令
#第四部执⾏ i++ =1
#接下来就重复 第⼆部-->第四部 ---直到条件最终不成⽴停⽌
[root@bgx shell]# sh for-9.sh
num is 1 9
num is 2 8
num is 3 7
num is 4 6
num is 5 5
num is 6 4
num is 7 3
num is 8 2
num is 9 1
语法六:
[root@web01 shell-for]# cat for06.sh
#!/bin/bash
a=0
b=10
for i in {1..9}
do
let a++;
let b--;
echo Number is $a $b
done
示例:
需求一:批量探测某个⽹段的主机存活状态,将存活的主机存⼊ok.txt⽂件中。
[root@web01 shell-for]# cat for08.sh
#!/bin/bash
for i in {1..254}
do
{
ip=10.0.0.$i
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip 存活"
fi
}&
done
需求二:判断主机存活状态,要求判断三次,如果三次失败则失败。
[root@web01 shell-for]# cat for09.sh
#!/bin/bash
for i in {1..254}
do
{
ip=10.0.0.$i
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip 存活" >>ok.txt
else #如果ip不存活,则进⼊else阶段
for j in {1..3} #循环总共要运⾏3次(如果三次都结束就退
出当前的循环)
do
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "探测$ip $j 次,成功" >> ok.txt
break #退出本次循环
else
echo "探测$ip $j 次,还是失败" >> err.txt
fi
done
fi
}&
done
wait
需求三: 嵌套循环:
现在有⼀个ip.txt的⽂件,⾥⾯有很多IP地址。还有⼀个port.txt的⽂件,⾥⾯有很多端⼝号。
现在希望对ip.txt的每个IP地址进⾏端⼝的探测,探测的端⼝号来源于port.txt⽂件中,最后将开放的端⼝和IP保存到⼀个ok.txt⽂件。
想法:
1.拿到ip.txt中的第⼀个IP地址。
2.基于已经拿到的IP进⾏端⼝的探测。
[root@web01 shell-for]# cat ip.txt
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
[root@web01 shell-for]# cat port.txt
80
22
3306
23
443
[root@web01 shell-for]# cat for15.sh
#!/bin/bash
#第⼀层循环
for ip in $(cat ip.txt)
do
{
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
#第⼆次循环
for port in $(cat port.txt)
do
nc -z -w 1 $ip $port &>/dev/null
if [ $? -eq 0 ];then
echo "$ip $port" >> ok.txt
fi
done
else
echo "$ip 不通所以没有进⾏端⼝探测.."
fi
}&
done
wait #等待任务执⾏完毕后,在释放窗⼝的操作
需求四:获取系统的所有⽤户并输出。效果如下:
This is 1 user: root
This is 2 user: bin
This is 3 user: daemon
This is 4 user: adm
...............
[root@web01 shell-for]# cat for10.sh
#!/bin/bash
i=1
for user in $(awk -F ":" '{print $1}' /etc/passwd)
do
echo This is $i user: $user
let i++ #让变量进⾏⾃增+1
done
需求五:批量创建100个⽤户,⽐如输⼊oldxu则会创建oldxu001-100。
1.交互输⼊还是脚本传参 read | $1
2.⽤户填写oldxu
3.执⾏for循环进⾏批量创建⽤户
[root@web01 shell-for]# cat for11.sh
#!/bin/bash
. /etc/init.d/functions
read -p "请输⼊你要创建的⽤户名称: " us
for i in {001..100}
do
#将⽤户传递的名称+编号组成需要创建的⽤户名称
username=${us}${i}
id $username &>/dev/null
if [ $? -eq 1 ];then
useradd $username
action "$username is create ok" /bin/true
else
action "$username already exists" /bin/false
fi
done
需求六:批量删除100个⽤户,⽐如输⼊oldxu则会删除oldxu001-100。
[root@web01 shell-for]# cat for12.sh
#!/bin/bash
. /etc/init.d/functions
read -p "请输⼊你要创建的⽤户名称: " us
for i in {001..100}
do
#将⽤户传递的名称+编号组成需要创建的⽤户名称
username=${us}${i}
id $username &>/dev/null
if [ $? -eq 0 ];then
userdel $username
action "$username is delete ok" /bin/true
else
action "$username is delete err" /bin/false
fi
done
需求七:使用case批量创建-删除100个⽤户。
[root@web01 shell-for]# cat for13.sh
#!/bin/bash
. /etc/init.d/functions
if [ $# -ne 2 ];then
echo "请在执⾏脚本时进⾏参数传递 [ --del Username | --add Username ] "
exit
fi
case $1 in
add|--add)
for i in {1..100}
do
username=$2$i
useradd $username &>/dev/null
if [ $? -eq 0 ];then
action "添加 $username 成功" /bin/true
else
action "添加 $username 失败" /bin/false
fi
done
;;
del|--del)
for i in {1..100}
do
username=$2$i
userdel $username &>/dev/null
if [ $? -eq 0 ];then
action "删除 $username 成功" /bin/true
else
action "删除 $username 失败" /bin/false
fi
done
;;
*)
echo "USAGE: sh $0 [ --add | --del | add | del ]"
exit
esac
需求八:通过读⼊⽂件中的⽤户与密码⽂件,进⾏批量添加⽤户。
⽂件中的格式: user:passwd
[root@web01 shell-for]# cat user.txt
xiaohong:123
xiaolan:456
xiaotian:789
xiaowang:012
xiaogou:345
想法:
1.使⽤for循环如何读⼊⽂件内容:
2.for循环如何将这⾥的每⼀列进⾏拆分
[root@web01 shell-for]# cat for14.sh
#!/bin/bash
for i in $(cat user.txt)
do
user=$(echo $i | awk -F ":" '{print $1}') #将⼀⾏中的第⼀列赋值给user
变量
pass=$(echo $i | awk -F ":" '{print $2}') #将⼀⾏中的第⼆列赋值给pass
变量
#创建⽤户
useradd $user &>/dev/null && \
echo "$pass" | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user 创建成功."
else
echo "$user 创建失败."
fi
done