05Shell循环语句
循环语句
for
语法结构
for 变量名 [ in 取值列表 ]
do
循环体
done
注意 当for对文件内容进行逐行处理时,会忽略空行
示例
例1
ping 主机的脚本(初始版):缺点执行过程慢,Ctrl+C只能结束某一个循环,并不能结束脚本
[root@hadoop04 shell_for]# vim ping.sh
#!/usr/bin/bash
##########################################
# ping test #
# v1.0 by ElegantSmile 9/12/2019 #
##########################################
reset_col="\e[0m"
red_col="\e[31m"
green_col="\e[32m"
# 通过输出重定向,在ping之前清理ip_up.txt和ip_down.txt的内容
>ip_up.txt
>ip_down.txt
# 产生序列有两种方式
# {n..m}
# `seq n m`
# n<m
for i in `seq 200`
for i in `seq 200`
do
ip=172.22.34.${i}
ping -c1 -W1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo -e "${green_col}${ip} is up${reset_col}" | tee -a ip_up.txt
else
echo -e "${red_col}${ip} is down${reset_col}" | tee -a ip_down.txt
fi
done
例2
ping 主机的脚本(改进版):每一个循环放入一个子shell中执行,可以大大地加快脚本执行的速度
注意 1.将循环放到后台运行 {}& 2.wait 3.执行脚本前清理文件内容
[root@hadoop04 shell_for]# vim ping01.sh
#!/usr/bin/bash
##########################################
# ping test #
# v1.1 by ElegantSmile 9/12/2019 #
##########################################
# 通过输出重定向,在ping之前清理ip_up.txt和ip_down.txt的内容
>ip_up.txt
>ip_down.txt
reset_col="\e[0m"
red_col="\e[31m"
green_col="\e[32m"
for i in `seq 100 200`
do
# 将循环放到后台进程执行
{
ip=172.22.34.${i}
ping -c1 -W1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo -e "${green_col}${ip} is up${reset_col}" | tee -a ip_up.txt
else
echo -e "${red_col}${ip} is down${reset_col}" | tee -a ip_down.txt
fi
}&
done
#等待前面的所有后台进程结束
wait
echo "finish..."
两个版本的脚本执行时间比较
改进版
[root@hadoop04 shell_for]# time bash ping01.sh
(略)
finish...
real 0m1.208s
user 0m0.338s
sys 0m0.781s
初始版
[root@hadoop04 shell_for]# time bash ping.sh
(略)
real 2m16.459s
user 0m0.277s
sys 0m0.677s
例3
ping 指定主机
# 指定主机的IP地址
[root@hadoop04 shell_for]# vim ip.txt
172.22.34.18
172.22.34.89
172.22.34.56
172.22.34.192
172.22.34.94
172.22.34.243
# 编写脚本
[root@hadoop04 shell_for]# vim ping02.sh
#!/usr/bin/bash
##########################################
# ping test #
# v1.2 by ElegantSmile 9/12/2019 #
##########################################
ip_txt=$1
reset_col="\e[0m"
red_col="\e[31m"
green_col="\e[32m"
for ip in `cat ${ip_txt}`
do
{
ping -c1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo -e "${green_col}${ip} is up${reset_col}"
else
echo -e "${red_col}${ip} is down${reset_col}"
fi
}&
done
wait
echo "finish..."
# 执行脚本
[root@hadoop04 shell_for]# time bash ping02.sh ip.txt
172.22.34.18 is up
172.22.34.56 is down
172.22.34.192 is down
172.22.34.94 is down
172.22.34.243 is down
172.22.34.89 is down
finish...
real 0m3.018s
user 0m0.008s
sys 0m0.017s
例4
创建用户,输入前缀、密码、数量;确认输入的信息;根据用户存在与否,提示已经存在或者创建用户
[root@hadoop04 shell_for]# vim create_user01.sh
!/usr/bin/bash
##########################################
# add user #
# v1.0 by ElegantSmile 9/12/2019 #
##########################################
while :
do
read -p "Please enter prefix & password & num[tianyun 123 5]" prefix pass num
printf "user information:
--------------------------
user prefix: ${prefix}
user password: ${pass}
user number: ${num}
--------------------------
"
read -p "Are you sure?[y|n]: " action
if [ "${action}" = "y" ];then
break
fi
done
# seq -w 等位补齐
for i in `seq -w ${num}`
do
username=${prefix}${i}
id ${username} &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} already exists"
else
useradd ${username}
echo "${pass}" | passwd --stdin ${username} &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} is created"
fi
fi
done
例5
创建用户,通过文件指定要创建的用户和密码
# 编辑用户信息文件user.txt
[root@hadoop04 shell_for]# vim user.txt
alice 123123
jeve 7dsgf9
# 编写脚本
[root@hadoop04 shell_for]# vim create_user02.sh
#!/usr/bin/bash
##########################################
# add user #
# v1.1 by ElegantSmile 10/12/2019 #
##########################################
# 执行脚本时,必须传入一个参数
if [ $# -eq 0 ];then
echo "usage: `basename $0` file"
exit 1
fi
# 执行脚本时,必须传入一个参数
if [ $# -eq 0 ];then
echo "usage: `basename $0` file"
exit 1
fi
# 执行脚本时,传入的参数必须是文件
if [ ! -f $1 ];then
echo "error file"
exit 2
fi
# 定义变量
user_text=$1
#IFS 内部字段分隔符
IFS=$'\n'
for line in `cat "${user_text}"`
do
# 获取指定用户的用户名和密码
username=`echo "${line}" | awk '{print $1}'`
userpass=`echo "${line}" | awk '{print $2}'`
id ${username} &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} already exists"
continue
fi
useradd ${username} &> /dev/null && echo "${userpass}" | passwd --stdin ${username} &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} is created"
echo "userpassword: ${userpass}"
fi
done
例6
批量修改主机密码
while
语法结构
while 条件测试
do
循环体
done
==当条件测试成立(条件测试为`真`),执行循环体
注意
☆☆☆当需要对文件内容进行逐行处理时,推荐使用:
while read line
do
循环体
done < FILE
示例
例1
创建用户,通过用户列表文件创建用户
# 编辑用户信息文件user.txt
[root@hadoop04 shell_while]# vim user.txt
alice 123123
jeve 7dsgf9
# 编写脚本
[root@hadoop04 shell_while]# vim create_user01.sh
#!/usr/bin/bash
##########################################
# add user #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
while read userinfo
do
if [ ${#userinfo} -eq 0 ] ;then
echo "Nothing to do"
continue
fi
username=`echo "${userinfo}" | awk '{print $1}'`
userpass=`echo "${userinfo}" | awk '{print $2}'`
id "${username}" &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} already exists" else
useradd ${username} &> /dev/null && echo "${userpass}" | passwd --stdin ${username} &> /dev/null
if [ $? -eq 0 ];then
echo "user ${username} is created"
echo "userpassword: ${userpass}"
echo "username: ${username}"
fi
fi
done < user.txt
echo "all ok..."
until
语法结构
until 条件测试
do
循环体
done
==当条件测试成立(条件测试为`假`),执行循环体
示例
ping主机,检查IP状态
# while循环
# ip ping不通时提示
# 类似于下线提示
[root@hadoop04 shell_until]# vim ping01.sh
#!/usr/bin/bash
##########################################
# ping hosts #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
ip=$1
while ping -c1 -W1 ${ip}&> /dev/null:
do
sleep 1
done
echo "${ip} is down"
# until循环
# ip ping得通时提示
# 类似于上线提示
[root@hadoop04 shell_until]# vim ping02.sh
#!/usr/bin/bash
##########################################
# ping hosts #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
ip=$1
until ping -c1 -W1 ${ip}&> /dev/null:
do
sleep 1
done
echo "${ip} is up"
For While Until对比
循环次数固定的 --> for
循环次数不固定的 --> while until
对文件逐行处理 --> while
ping主机探测
循环次数固定的ping主机探测
For
[root@hadoop04 shell_for_while_until]# vim for_while_until_ping.sh
#!/usr/bin/bash
##########################################
# ping hosts #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
for i in {2..254}
do
{
ip=172.22.145.${i}
ping -c1 -W1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo "${ip} is up"
fi
} &
done
wait
echo "all finish..."
While
[root@hadoop04 shell_for_while_until]# vim while_until_for_ping.sh
#!/usr/bin/bash
##########################################
# ping hosts #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
i=2
while [ $i -le 254 ]
do
{
ip=172.22.145.${i}
ping -c1 -W1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo "${ip} is up"
fi
} &
let i++
done
wait
echo "all finish..."
Until
[root@hadoop04 shell_for_while_until]# vim until_for_while_ping.sh
#!/usr/bin/bash
##########################################
# ping hosts #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
i=2
until [ $i -gt 254 ]
do
{
ip=172.22.145.${i}
ping -c1 -W1 ${ip} &> /dev/null
if [ $? -eq 0 ];then
echo "${ip} is up"
fi
} &
let i++
done
wait
echo "all finish..."
计算1-100的和
For
[root@hadoop04 shell_for_while_until]# vim for_while_until_sum100.sh
#!/usr/bin/bash
##########################################
# sum 100 #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
for i in {1..100}
do
#let sum=${sum}+${i}
let sum+=${i}
done
echo "sum: ${sum}"
While
[root@hadoop04 shell_for_while_until]# vim while_until_for_sum100.sh
#!/usr/bin/bash
##########################################
# sum 100 #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
i=1
while [ ${i} -le 100 ]
do
#let sum=${sum}+${i}
let sum+=${i}
let i++
done
echo "sum: ${sum}"
Until
[root@hadoop04 shell_for_while_until]# vim until_for_while_sum100.sh
#!/usr/bin/bash
##########################################
# sum 100 #
# v1.0 by ElegantSmile 10/12/2019 #
##########################################
i=1
until [ ${i} -gt 100 ]
do
#let sum=${sum}+${i}
let sum+=${i}
let i++
done
echo "sum: ${sum}"
05Shell循环语句的更多相关文章
- 【python之路4】循环语句之while
1.while 循环语句 #!/usr/bin/env python # -*- coding:utf-8 -*- import time bol = True while bol: print '1 ...
- python之最强王者(3)——变量,条件、循环语句
1.Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的 ...
- #9.5课堂JS总结#循环语句、函数
一.循环语句 1.for循环 下面是 for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块) ...
- 详解Python中的循环语句的用法
一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...
- 【java开发】分支语句、循环语句学习
一.Java分支语句类型 if-else 语句 switch 关于if-esle语句可以拆分为三种 if语句 if(条件){语句块;} if-else语句if(条件语句){语句块;} if-else ...
- python3循环语句while
Python的循环语句有for和while语句,这里讲while语句. Python中while语句的一般形式: while 条件判断 : 语句 需要注意冒号和缩进.另外,注意Python中没有do. ...
- 20.SqlServer中if跟循环语句
--if语句declare @i int begin print @i end else --循环语句 declare @i int begin insert into grade(classname ...
- Python学习【第五篇】循环语句
Python循环语句 接下来将介绍Python的循环语句,程序在一般情况下是按顺序执行的. 编程语言提供了各种控制结构,允许更复杂的执行路径. 循环语句允许我们执行一个语句或语句组多次. Python ...
- iOS -Swift 3.0 -for(循环语句用法)
// // ViewController.swift // Swift-循环语句 // // Created by luorende on 16/12/08. // Copyright © 2016年 ...
随机推荐
- Oracle DB Time
Oracle DB Time是Oracle数据库在时间维度上剖析性能的一个重要指标,通过逐级分解该指标,定位到浪费资源或者资源争用的首要事件上,从而通过减少等待以及最小化每个请求的使用资源来达到优化的 ...
- datatable的dom配置
l - Length changing 改变每页显示多少条数据的控件 f - Filtering input 即时搜索框控件 t - The Table 表格本身 i - Information 表格 ...
- Java中文本文件的读取(按行读取)
在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中.每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程.这样周而复始,并没 ...
- [考试反思]1114csp-s模拟测试115:零迟
最后一次了,允许自己混进榜里吧. 没有心态,原题不会做(真的忘了) T2的搜索没有分. 「 零 · 迟 」:酷刑 只有在最后的时刻才开始意识到,一切的一切都已经晚了. 就在眼前了.没有机会了. 退役, ...
- IDEA生成可执行的jar文件
场景 用IDEA开发一个Java控制台程序,项目完成后,打包给客户使用. 做法 首先用IDEA打开要生成jar的项目,打开后选择File->Project Structure... 选择Arti ...
- 【第二章】Zabbix3.4监控SQLServer数据库和H3C交换机思科Cisco防火墙交换机教程笔记
监控SQLServer数据库 SSMS执行相关SQL SQL模板命名规则 Zabbix客户端导入模板 添加SQLServer监控图形 SQLServer服务器关联模板 监控思科Cisco防火墙交换机 ...
- Redis缓存与数据库一致性解决方案
背景 缓存是数据库的副本,应用在查询数据时,先从缓存中查询,如果命中直接返回,如果未命中,去数据库查询最新数据并返回,同时写入缓存. 缓存能够有效地加速应用的读写速度,同时也可以降低后端负载.是应用架 ...
- 深度学习VGG16模型核心模块拆解
原文连接:https://blog.csdn.net/qq_40027052/article/details/79015827 注:这篇文章是上面连接作者的文章.在此仅作学习记录作用. 如今深度学习发 ...
- SPARQL入门(二)使用Java操作ARQ
在文章SPARQL入门(一)SPARQL简介与简单使用中,我们了解了RDF.SPARQL以及基于Java编写的SPARQL处理器ARQ.在本文中,笔者将会如何使用Java来操作ARQ. 注意到 ...
- (三十六)c#Winform自定义控件-步骤控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...