好程序员云核算教程同享Shell编程之for循环结构

好程序员云<a href="https://www.fons.com.cn/tag/%e6%a0%b8%e7%ae%97" target="_blank">核算教程同享Shell编程之for循环结构
No.1 for循环结构

for i in {取值规划}
do
循环体
done
求取1-100数字的和、找出1-100中能被2整除的数字、找出1-100中的素数、找出1-100种能被3整除的数字

#!/usr/bin/env bash
#

Author: bavdu

Email: bavduer@163.com

Github: https://github.com/bavdu

Date: 2019//


sum=0
for i in {1..100}
do
      let total=$sum+$i #或许写成let total=i++
done
printf "$totaln"
查验出产环境中的主机存活性

#!/usr/bin/env bash
#

Author: bavdu

Email: bavduer@163.com

Github:

Date: 2019//

ip_alive.txt
ip_down.txt

export segment="192.168.161"
for i in {2..254}
do
{
       ping -c1 $segment.$i &>/dev/null
       if [ $? -eq 0 ];then
      printf "alive: $segment.$in" >>ip_alive.txt
       else
      printf "down: $segment.$in" >>ipdown.txt
      fi
}&
done
wait
printf "$(date +%Y-%m-%d
%k:%M:%S) -exec filename:$0n"