(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循环的更多相关文章

  1. 孤荷凌寒自学python第十五天python循环控制语句

    孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...

  2. Linux Shell系列教程之(十)Shell for循环

    本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shel ...

  3. 第十节,While循环和for循环

    While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假()不循环 格式: While 条件: 代码块 注意:在While循环里如 ...

  4. 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题

    foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部变量,这个局部变量的类型与part2中的对 ...

  5. flask学习(十二):for循环遍历

    一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...

  6. 练习五十六:for循环

    某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换 方法一: def o ...

  7. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

  8. DeepLearning.ai学习笔记(五)序列模型 -- week1 循环序列模型

    一.为什么选择序列模型 序列模型可以用于很多领域,如语音识别,撰写文章等等.总之很多优点... 二.数学符号 为了后面方便说明,先将会用到的数学符号进行介绍. 以下图为例,假如我们需要定位一句话中人名 ...

  9. python之路---03 整型 bool 字符串 for循环

    十三.整型(int) 基本操作: 1.+ - * / % // ** 2.  .bit_length() 计算整数在内存中占⽤的⼆进制码的⻓度 如: 十四.布尔值(bool) True  False ...

  10. (转)Linux Shell系列教程之(十四) Shell Select教程

    本文属于<Linux Shell 系列教程>文章系列,该系列共包括以下 18 部分: Linux Shell系列教程之(一)Shell简介 Linux Shell系列教程之(二)第一个Sh ...

随机推荐

  1. grpc deadlines

    最近在将应用的rpc更换为grpc,使用过程中,发现报“rpc error:code=DeadlineExceeded desc = context deadline exceeded”,这是啥?原来 ...

  2. can be found for element 'tx:annotation-driven'

    错误描述: ERROR [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (ContextLoader.java:308) - Cont ...

  3. 使用 Redis的SETNX命令实现分布式锁

    使用Redis的 SETNX 命令可以实现分布式锁,下文介绍其实现方法. SETNX命令简介 命令格式 SETNX key value 将 key 的值设为 value,当且仅当 key 不存在. 若 ...

  4. nowcoder 提高组模拟赛 最长路 解题报告

    最长路 链接: https://www.nowcoder.com/acm/contest/178/A 来源:牛客网 题目描述 有一张 \(n\) 个点 \(m\) 条边的有向图,每条边上都带有一个字符 ...

  5. HDU 2126 01背包(求方案数)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. NOIP2016愤怒的小鸟 [状压dp]

    愤怒的小鸟 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Kiana 可以用它向第一象限发射一只红色的小鸟, ...

  7. 怎么给word加底纹

  8. Git命令文本手册

    git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...

  9. jquery 的相关 width 和 height 方法辨析

    width() 设置或返回元素的宽度(不包括内边距.边框或外边距). height() 设置或返回元素的高度(不包括内边距.边框或外边距). innerWidth() 返回元素的宽度(包括内边距). ...

  10. 图论:2-SAT

    先象征性地描述一下问题:一组(或者一个)东西有且仅有两种选择,要么选这个,要么选那个,还有一堆的约束条件 图论问题,当然是建边跑图喽 给出模型: 模型一:两者(A,B)不能同时取 那么选择了A就只能选 ...