(十)while和until循环
(1)while循环
语法:当条件测试成立(真),执行循环体
while 条件测试
do
循环体
done
1)while批量创建用户1
从user.txt读取到的行数据赋予给变量user值
#!/bin/bash
while read user
do
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user is already exists!"
else
useradd $user
if [ $? -eq 0 ];then
echo "user $user is created!"
fi
fi
done <user.txt
cat user.txt
jack01
jack02
jack03
jack04
2)while批量创建用户2
while默认空格作为分割,非常适合处理文件
#!/bin/bash
while read line
do
{
user=$(echo $line|awk '{print $1}')
pass=$(echo $line|awk '{print $2}')
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user is already exists!"
else
useradd $user
echo $pass | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user is created!"
fi
fi
}&
done <$1
wait
echo "all ok....."
# cat user2.txt
jack001 123
jack002 123
jack03 123
jack04 123
3)while对有空行文件的处理方式
#!/bin/bash
while read line
do
if [ ${#line} -eq 0 ];then
continue
fi
{
user=$(echo $line|awk '{print $1}')
pass=$(echo $line|awk '{print $2}')
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user is already exists!"
else
useradd $user
echo $pass | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user is created!"
fi
fi
}&
done <$1
wait
echo "all ok....."
(2)until循环
语法:当条件测试成立(假),执行循环体
until 条件测试
do
循环体
done
#!/bin/bash
ip=114.114.114.114
until ping -c1 -W1 $ip &>/dev/null
do
sleep 1
done
echo "$ip is up"
(3)for,while,until 循环ping比较
#!/bin/bash
for i in {2..209}
do
{
ip=192.168.111.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
fi
}&
done
wait
echo "all done...."
#!/bin/bash
i=2
while [ $i -le 254 ]
do
{
ip=192.168.111.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
fi
}&
let i++
done
wait
echo "all done...."
#!/bin/bash
i=2
until [ $i -gt 254 ]
do
{
ip=192.168.111.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
fi
}&
let i++
done
wait
echo "all done...."
(4)for,while,until 循环求1到100的和
#!/bin/bash
for i in {1..100}
do
#let sum=$sum+$i
let sum+=$i
done
echo "sum:$sum"
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
#let sum=$sum+$i
let sum+=$i
let i++
done
echo "sum:$sum"
#!/bin/bash
i=1
until [ $i -gt 100 ]
do
#let sum=$sum+$i
let sum+=$i
let i++
done
echo "sum:$sum"
总结:循环次数固定建议使用for循环,循环文件建议使用while循环,循环次数不固定建议使用while和until循环
(十)while和until循环的更多相关文章
- 孤荷凌寒自学python第十五天python循环控制语句
孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...
- Linux Shell系列教程之(十)Shell for循环
本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shel ...
- 第十节,While循环和for循环
While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假()不循环 格式: While 条件: 代码块 注意:在While循环里如 ...
- 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题
foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部变量,这个局部变量的类型与part2中的对 ...
- flask学习(十二):for循环遍历
一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...
- 练习五十六:for循环
某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换 方法一: def o ...
- Linux Shell系列教程之(十二)Shell until循环
本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...
- DeepLearning.ai学习笔记(五)序列模型 -- week1 循环序列模型
一.为什么选择序列模型 序列模型可以用于很多领域,如语音识别,撰写文章等等.总之很多优点... 二.数学符号 为了后面方便说明,先将会用到的数学符号进行介绍. 以下图为例,假如我们需要定位一句话中人名 ...
- python之路---03 整型 bool 字符串 for循环
十三.整型(int) 基本操作: 1.+ - * / % // ** 2. .bit_length() 计算整数在内存中占⽤的⼆进制码的⻓度 如: 十四.布尔值(bool) True False ...
- (转)Linux Shell系列教程之(十四) Shell Select教程
本文属于<Linux Shell 系列教程>文章系列,该系列共包括以下 18 部分: Linux Shell系列教程之(一)Shell简介 Linux Shell系列教程之(二)第一个Sh ...
随机推荐
- typescript 贪吃蛇[学习过程中,模仿的一个例子]
代码实现ts: 1 'use strict' module Main { const FloorType = { space: "space", snack: "body ...
- $.ajax()方法参数总结
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和d ...
- DataTable定义
DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表.).DataTable是ADO dot net 库中的核心对象.它可以被应用在 VB 和 ASP 上.它无须代码就可以简单的绑 ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- LeetCode -- 3SumCloset
Question: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- (转载)Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- The xor-longest Path [Trie]
The xo-longest Path 题目描述 给定一棵\(n≤100 000\)个点的带权树,求树上最长的异或和路径. 输入 多组数据.每组数据第一行一个整数n(\(1≤n≤100 00\),接下 ...
- POJ3020:Antenna Placement(二分图匹配)
Antnna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11093 Accepted: 5459 ...
- HDU 多校对抗赛第二场 1004 Game
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- centos配置数据源和java环境配置
---恢复内容开始--- 一:前言 今天送走了一位同事,看着别人走勾起了我蠢蠢欲动的心啊,但是我知道,我不能那么的任性,我是men,这几天难得的清闲,所以我就弄一弄linux,昨天把网给配通了,今天配 ...