for循环语句_基础

for循环语句格式:

#!/bin/bash
for 男人 in 世界                 #世界上所有的男人过滤
do
if [有房] || [有车] || [有存款] || [会做饭] || [会做家务];then    #或者满足这些条件中的一个

echo "我喜欢"      #我喜欢
else
rm -rf 男人           #杀死这男的
fi

done

最简单的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

 案例一:使用for循环、while循环实现 判断一个网段ip的存活数

1.使用while循环实现

#!/bin/bash
IP=192.168.0.
ip=1
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
while [ $ip -lt 255 ]
do
ping $IP$ip -c 2|grep -q "ttl=" #&& echo "192.168.0.$ip yes" || echo "192.168.0.$ip no"
if [ $? -eq 0 ];then
echo "$IP$ip yes !"
else
echo "$IP$ip no !"
fi

#ip=$(($ip + 1))
ip=$(expr "$ip" "+" "1")
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行结束......"

2.使用for循环实现

#!/bin/bash
IP=192.168.0.
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
#for i in $(seq 1 254)

for ((i=1;i<=254;i++))

do
ping $IP$i -c 2 | grep -q "ttl="
if [ $? -eq 0 ];then
echo "$IP$i yes !"
else
echo "$IP$i no !"
fi
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行结束......"

其他知识点:

#!/bin/bash

for num in 192.168.0.1 192.168.0.2
do
echo $num
done

#echo 命令的特殊用法

echo {1..5}
echo {5..1}
echo {a..z}

seq命令的特殊用法
seq -s " " 1 5
seq -s " " 5 -1 1

for num in `seq -s " " 1 5`
do
echo $num
done

for num in `seq -s " " 5 -1 1`
do
echo $num
done

#查看当前目录下的目录

for i in `ls -F|grep /`
do
echo "$i"
done

使用for循环批量修改文件名称

使用字符串拼接的方法批量修改文件名

ls *.txt|awk -F ".txt" '{print "mv" " " $0 " " $1 ".TXT"}'|bash

ls *.TXT|awk -F ".TXT" '{print "mv " $0 " " $1 ".txt"}'|bash

#sed命令将每行的最后一个?给替换成??

sed -e 's/(.*)?/1??/'

echo `awk '{print $10}' access_log|tr " " "+"`|sed -e 's/(.*)+/1 /'|bc

while循环计算apache日志文件第十列的总和

案例一:

#!/bin/bash
sum=0
file=/var/log/httpd/access_log
while read line
do
size=`echo $line|awk '{print $10}'`
# [ "$size" == "-" ] && continue
((sum=sum+$size))
done</var/log/httpd/access_log
[ -n "$sum" ] && echo "$sum"

案例二:使用for循环批量创建用户

1.创建随机的8位密码 (数字+字母)

 echo $(date +%t%N)$RANDOM|md5sum|cut -c 1-8

批量删除用户脚本

for i in $(seq -w 10);do userdel -r oldboy-$i;done

 批量添加用户脚本

#!/bin/bash
user_passwd_file=/root/oldboy.txt
not_user_passwd_file=/root/fail_oldboy.txt

##################################

if [ -f $user_passwd_file ];then
:> $user_passwd_file
else
echo "$user_passwd_file does not exist"
fi

if [ -f $not_user_passwd_file ];then
:> $not_user_passwd_file
else
echo "$not_user_passwd_file does not exist"
fi

. /etc/init.d/functions
for i in $(seq -w 10)
do
passwd=$(echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9)
useradd oldboy-$i >&/dev/null && user_status=$?
echo "$passwd"|passwd --stdin oldboy-$i >&/dev/null && passwd_status=$?

if [ $user_status -eq 0 -a $passwd_status -eq 0 ];then
action "adduser oldboy-$i" /bin/true
echo -e "user: oldboy-$i passwd:$passwd" >>$user_passwd_file
else
action "adduser oldboy-$i" /bin/false
echo -e "user: oldboy-$i passwd:$passwd" >>$not_user_passwd_file
fi
done

案例三:成绩分类脚本

#!bin/bash
while true
do
read -t10 -p "请输入学号:" number
if [ $number == exit ];then
exit
fi
echo "$number"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
echo "this is number !"
else
exit
fi
read -t10 -p "请输入姓名:" name
echo "$name"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
exit
else
echo "this is character !"
fi
read -t10 -p "请输入成绩:" achievement
echo "$achievement"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
echo "this is number !"
else
exit
fi
if [ "$achievement" -ge "60" ];then
echo "学号=$number 姓名=$name 成绩=$achievement" >> pass.txt
else
echo "学号=$number 姓名=$name 成绩=$achievement" >> notpass.txt
fi
done

案例四:使用for循环和while循环从1加到100

#!/bin/bash
for ((i=0;i<=100;i++))
do
((j=j+i))
done
echo $j

#!/bin/bash
i=1
sum=0
while ((i <= 100))
do
((sum=sum+i))
((i++))
done
#echo $sum
[ -n "$sum" ] && printf "totalsum is: $sum "

n=0
while ((n<=100))
do
((s=s+n))
((n++))
done
echo $s

案例五:

while循环检测网站访问是否正常。(人性化输出)

使用方法:sh nginx_server1.sh 192.168.0.130

#!/bin/bash
. /etc/rc.d/init.d/functions
while true
do
status=`curl -I -s --connect-timeout 10 $1|head -1|cut -d" " -f 2`
if [ "$status" = "200" ];then
action "nginx this is yes !" /bin/true
else
action "nginx this is no !" /bin/false
fi
sleep 2

(监控脚本化输出)

#!/bin/bash
while true
do
status=`curl -I -s --connect-timeout 10 $1|head -1|cut -d " " -f 2`
if [ "$status" == "200" ];then
echo "this url is ok !"
else
echo "this url is no !"
fi
sleep 2
done

案例六:每隔2秒打印服务器的负载情况

#!/bin/bash
while true
do
uptime  >> /var/log/fuzai.log
sleep 2
done

原文地址:https://www.cnblogs.com/xiaoyongzhuo/p/7412926.html