这篇文章是云计算学习路线教程代码笔记关于Shell编程for循环结构的内容。

for i in {取值范围}

do

循环体

done

求取1-100数字的和、找出1-100中能被2整除的数字、找出1-100中的素数、找出1-100种能被3整除的数字

#!/usr/bin/env bash

#

Author: bavdu

Email: [email protected]

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: [email protected]

Github: https://github.com/bavdu

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"