在编写脚本时,一定要注意空格

基本语法:

while [ condition ]
do
command1
command2
command3
done
condition为true时命令1到命令3将会一直执行,知道条件为false ,例如:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done

Here is a sample shell code to calculate factorial using while loop:

#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial

To run just type:
$ chmod +x script.sh
$ ./script.sh 5

Output:

120

While loops are frequently used for reading data line by line from file:

#!/bin/bash
FILE=$1
# read $FILE using the file descriptors
exec 3<&0
exec 0<$FILE
while read line
do
# use $line variable to process line
echo $line
done
exec 0<&3

You can easily evaluate the options passed on the command line for a script using while loop:

......
..
while getopts ae:f:hd:s:qx: option
do
case "${option}"
in
a) ALARM="TRUE";;
e) ADMIN=${OPTARG};;
d) DOMAIN=${OPTARG};;
f) SERVERFILE=$OPTARG;;
s) WHOIS_SERVER=$OPTARG;;
q) QUIET="TRUE";;
x) WARNDAYS=$OPTARG;;
\?) usage
exit 1;;
esac
done
.......
..

How do I use while as infinite loops?

Infinite for while can be created with empty expressions, such as:

#!/bin/bash
while :
do
echo "infinite loops [ hit CTRL+C to stop]"
done

Conditional while loop exit with break statement

You can do early exit with the break statement inside the whil loop. You can exit from within a WHILE using break. General break statement inside the while loop is as follows:

while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the while lopp.
fi
statements3 #While good and, no disaster-condition.
done

In this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:

#!/bin/bash
 
while :
do
read -p "Enter two numnbers ( - 1 to quit ) : " a b
if [ $a -eq -1 ]
then
break
fi
ans=$(( a + b ))
echo $ans
done

Early continuation with the continue statement

To resume the next iteration of the enclosing WHILE loop use the continue statement as follows:

while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (condition)
then
continue #Go to next iteration of I in the loop and skip statements3
fi
statements3
done

while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the while lopp.
fi
statements3 #While good and, no disaster-condition.
done

bash shell while语法的更多相关文章

  1. Linux bash shell脚本语法入门

    1.基础 #!/bin/bash   //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 #                 //shell脚本注释符号 2.变量和使用 HOME= ...

  2. (转)Linux bash shell脚本语法入门

    http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...

  3. Bash shell编程的语法知识点(1)

    Bash shell脚本编程知识点如下(初学,不全,欢迎讨论补充): shell简介 脚本的简单介绍 变量和引用 算术运算 交互式编程 选择判断 条件测试 循环 函数 shell简介 shell是一种 ...

  4. bash shell最基本的语法

    1 shell语句的基本构成 shell每个基本的构成元素之间都相隔一个空格. 比如[ -e file ],[.-e.file.]这四个基本元素之间都相隔了一个空格. 同样的道理[ ! -e file ...

  5. bash shell命令(2)

    在上篇<bash shell命令(1)>中,介绍了几种简单的linux shell命令,今天继续介绍bash shell命令 本文地址:http://www.cnblogs.com/arc ...

  6. Shell函数语法

    Shell函数语法 定义函数: function   函数名(){ 指令... } 调用函数,方法1: 函数名 调用函数,方法2: 函数名  参数一   参数二 return在函数里面使用会跳出函数并 ...

  7. Linux Bash Shell 快速入门

    BASH 的基本语法 最简单的例子 —— Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1     ...

  8. 第四章:更多的bash shell命令

    第四章:更多的bash shell命令 监测程序 ps (其他ps内容见#1 ) Unix风格的ps命令参数 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程( ...

  9. shell脚本学习之Bash shell 里各种括号的用法

    今天在 SegmentFault 上看到又有人问起关于Shell里各种括号的问题.对于很多玩Shell的人,括号是个很尴尬的问题,用起来没问题,说起来不明白,我在这里总结一下Bash Shell几种括 ...

随机推荐

  1. python re模块findall使用

    今天练习re模块时候出现了一个很奇怪的问题,同样的正则表达式用re.search()与用re.compile().findall()匹配出来的结果不一致. 很是奇怪,故此记录一下,防止以后碰到类似情况 ...

  2. 洛谷P2319 [HNOI2006]超级英雄

    一开始是用二分图匹配(网络流)+二分做的,后来发现直接用匈牙利更简单 #include<cstdio> #include<cstdlib> #include<algori ...

  3. [BZOJ]4199: [Noi2015]品酒大会(后缀数组+笛卡尔树)

    Time Limit: 10 Sec  Memory Limit: 512 MB Description Input Output Sample Input 10 ponoiiipoi 2 1 4 7 ...

  4. 2015 多校联赛 ——HDU5363(快速幂)

    Problem Description soda has a set S with n integers {1,2,…,n}. A set is called key set if the sum o ...

  5. Codeforces Round #438 C. Qualification Rounds

    Description Snark and Philip are preparing the problemset for the upcoming pre-qualification round f ...

  6. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  7. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. 什么是Hash?

    什么是Hash? Hash中文翻译为散列,又成为"哈希",是一类函数的统称,其特点是定义域无限,值域有限.把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换 ...

  9. C语言程序设计第二次作业——顺序结构

    (一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. 错误信息1: 错误原因:i和d位置错误 改正方法:i和d位置互换 错误信息2: 错误原因:\n后缺了一个" 改正方法:\n后加一个 ...

  10. 以太坊区块链Java(EthereumJ)学习笔记:概述

    本系列文章介绍以太坊区块链基于Java语言的解决方案.通过介绍EthereumJ定义的主要模块和Class,希望为大家学习和使用EthereumJ提供一些帮助. 整体架构 以太坊的Java解决方案主要 ...