好程序员云计算教程分享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 "$total\n" 测试生产环境中的主机存活性
#!/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.$i\n" >>ip_alive.txt else printf "down: $segment.$i\n" >>ip_down.txt fi }& done wait printf "$(date +%Y-%m-%d_%k:%M:%S) -exec filename:$0\n"