(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. jetty-maven-plugin

    <plugins>   <plugin>    <groupId>org.eclipse.jetty</groupId>    <artifact ...

  2. 服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana

    服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana https://www.cnblogs.com/xishuai/p/elk- ...

  3. BZOJ4567 SCOI2016背单词(trie+贪心)

    倒过来变成查询前缀.考虑怎么排序.第一条代价n*n就相当于inf,说明一个单词的所有前缀都要排在它前面.那么串的依赖关系就是trie的结构.二三条说明代价是Σidi-idfa,那么显然最后的编号应该是 ...

  4. LeetCode -- Best Time to Buy and Sell Stock系列

    Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...

  5. vue父组件中修改子组件样式

    1. 使用全局样式 <style> /* 全局样式 */ </style> <style scoped> /* 本地样式 */ </style> 2. ...

  6. BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操 贪心+伪树dp+二分

    //论全局变量的杀伤力....QAQ#include<cstdio> #include<iostream> #include<cstdlib> #include&l ...

  7. Linux产生背景

    By francis_hao Oct 26,2016 很久很久以前,大概在1965年左右,由贝尔实验室(Bell).麻省理工学院(MIT)及通用电气公司(GE)共同发起了一个叫做Multics的项目, ...

  8. 杭电hdu 2089 数位dp

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...

  9. wget、yum、rpm、apt-get区别

    wget 类似于迅雷,是一种下载工具, 通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理 名字是World Wide Web”与“get”的结合. yum: 是 ...

  10. Java Error: Failed to validate certificate. The application will not be executed

    Hi, last week a customer had the problem that he wants to connect to the administration interface of ...