ITPub博客

首页 > 云计算 > 开源云工具 > 好程序员云计算教程分享Shell编程之for循环结构

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

原创 开源云工具 作者:好程序员IT 时间:2019-11-21 17:38:29 0 删除 编辑

好程序员云计算教程分享 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: 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"

 

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69913892/viewspace-2665218/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
欢迎关注公众号:好程序员特训营 web前端教程分享 723729549 大数据教程分享 703503210

注册时间:2019-03-20

  • 博文量
    539
  • 访问量
    410752