最简单的for循环语句
#!/bin/bash
for x in one two three four
do
echo number: $x
done
使用文件作为循环的参数
for i in /root/find/*
do
# echo $(basename $i) is a file living in /root/find
# echo $i
# echo $(basename $i)
echo `basename $i`
done
用seq的方式传递参数
for j in $(seq 1 5)
#for j in `seq 1 5`
do
echo $j
done
使用c语言的方式传递参数
for ((i=1;i<=5;i++))
do
echo "i=$i"
done
与while循环语句对比
num=1
while [ $num -le 10 ]
do
echo $num
num=$(($num + 1))
done
#!/bin/bash
ip=1
while [ $ip -lt 256 ]
do
ping 192.168.0.$ip -c 2 | grep -q "ttl=" #&& echo "192.168.0.$ip yes" || echo "192.168.0.$ip no"
if [ $? -eq 0 ];then
echo "192.168.0.$ip yes"
else
echo "192.168.0.$ip no"
fi
#ip=$(($ip + 1))
ip=`expr "$ip" "+" "1"`
done
#!/bin/bash
network=192.168.0.
IP=1
:>IP_use
:>IP_idle
:>ping_action
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
for i in $(seq 1 10)
do
ping $network$i -c 2 | grep -q "ttl="
if [ $? -eq 0 ]
then
echo "$network$i yes"
else
echo "$network$i no"
fi
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行完成......"