1、for 循环格式
fro 变量名 in 列表
do
循环体
done
2、for循环举例
示例(1)for循环定义列表演示
[root@node3 /server/scripts]# cat for1.sh
#!/usr/bin/bash
for var in file1 file2 file3
do
echo "this is $var"
done
[root@node3 /server/scripts]# sh for1.sh
this is file1
this is file2
this is file3
[root@node3 /server/scripts]#
示例(2)for循环定义列表演示
[root@node3 /server/scripts]# cat for2.sh
#!/usr/bin/bash
for var in file1 file2 file3
do
echo "this is 123"
done
[root@node3 /server/scripts]# sh for2.sh
this is 123
this is 123
this is 123
[root@node3 /server/scripts]#
示例(3)for循环定义列表演示
[root@node3 /server/scripts]# cat for3.sh
#!/bin/bash
for var in file1 "file2 file3" file4 "hello word"
do
echo "this is $var"
done
[root@node3 /server/scripts]# sh for3.sh
this is file1
this is file2 file3
this is file4
this is hello word
[root@node3 /server/scripts]#
示例(4)for循环定义列表演示
[root@node3 /server/scripts]# cat for4.sh
#!/bin/bash
for var in file1 file\'2 file\'s
do
echo "var is $var"
done
[root@node3 /server/scripts]# sh for4.sh
var is file1
var is file'2
var is file's
[root@node3 /server/scripts]#
示例(5)for循环定义列表演示
[root@node3 /server/scripts]# cat for5.sh
#!/bin/bash
list="file1 file2 file3"
for var in $list
do
echo "var is $var"
done
[root@node3 /server/scripts]# sh for5.sh
var is file1
var is file2
var is file3
[root@node3 /server/scripts]#
示例(6)IFS 定义for循环分隔符演示
[root@node3 /server/scripts]# cat for6.sh
#!/bin/bash
#默认for循环时以空格作为分隔符
#修改IFS变量,可以控制for循环的分隔符
IFS=$'\n' #以回车作为分隔符
for i in `cat /etc/hosts` #使用文件方式读入列表
do
echo "$i"
sleep 1
done
[root@node3 /server/scripts]# sh for6.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
[root@node3 /server/scripts]#
示例(7)IFS 定义for循环分隔符演示
[root@node3 /case/scripts/for]# cat for7.sh
#!/bin/bash
IFS=: #冒号做分隔符
list=$(head -1 /etc/passwd) #使用文件读入列表
for i in $list
do
echo $i
sleep 1
done
[root@node3 /case/scripts/for]# sh for7.sh
root
x
0
0
root
/root
/bin/bash
[root@node3 /case/scripts/for]#
示例(8)一种特殊(C语言方式)的使用for循环演示
[root@node3 /server/scripts]# cat for8.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
echo num is $i
done
[root@node3 /server/scripts]# sh for8.sh
num is 1
num is 2
num is 3
num is 4
num is 5
num is 6
num is 7
num is 8
num is 9
num is 10
[root@node3 /server/scripts]#
示例(9)for循环自增自减运算
[root@node3 /server/scripts]# cat for9.sh
#!/usr/bin/bash
for (( i=1,j=10;i<=10,j>=1;i++,j-- ))
do
echo num is $i + $j = $[ $i + $j ]
done
[root@node3 /server/scripts]# sh for9.sh
num is 1 + 10 = 11
num is 2 + 9 = 11
num is 3 + 8 = 11
num is 4 + 7 = 11
num is 5 + 6 = 11
num is 6 + 5 = 11
num is 7 + 4 = 11
num is 8 + 3 = 11
num is 9 + 2 = 11
num is 10 + 1 = 11
[root@node3 /server/scripts]#
示例(10) for自增自减运算
[root@node3 /case/scripts/for]# cat for9.sh
#!/bin/bash
a=0
b=10
for i in {1..9}
do
let a++;
let b--;
echo num is $a - $b
done
[root@node3 /case/scripts/for]# sh for9.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@node3 /case/scripts/for]#
示例(11)shell 并发执行
[root@node3 /case/scripts/for]# cat for10.sh
#!/bin/bash
#批量检测主机是否存活
>ip.txt
for ip in {1..254} #列表的生成方式
do
{
addr=10.0.0.$ip
ping -c 1 $addr &>/dev/null
if [ $? -eq 0 ];then
echo $addr | tee -a ip.txt
fi
}&
done
wait
echo "Get IP Is Finsh!!"
[root@node3 /case/scripts/for]# sh for10.sh
10.0.0.202
10.0.0.200
10.0.0.201
10.0.0.254
Get IP Is Finsh!!
[root@node3 /case/scripts/for]#cat ip.txt
10.0.0.200
10.0.0.201
10.0.0.202
10.0.0.254
示例(11)for 嵌套循环
[root@node3 /case/scripts/for]# cat for12.sh
#!/usr/bin/bash
#探测多个主机的多个不同端口是否存在
> all.txt
#1.遍历ip地址
for ip in $(cat ip.txt)
do
#2.遍历port文件循环
for port in $(cat port.txt)
do
{
nc -vz -w 1 $ip $port &>/dev/null
if [ $? -eq 0 ];then
echo "$ip $port" | tee -a all.txt
fi
}&
done
wait
done
[root@node3 /case/scripts/for]# sh for12.sh
10.0.0.200 80
10.0.0.200 22
10.0.0.201 22
10.0.0.201 80
10.0.0.202 22
[root@node3 /case/scripts/for]# cat all.txt
10.0.0.200 80
10.0.0.200 22
10.0.0.201 22
10.0.0.201 80
10.0.0.202 22
[root@node3 /case/scripts/for]#
网友评论