bash shell while语法
在编写脚本时,一定要注意空格
基本语法:
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语法的更多相关文章
- Linux bash shell脚本语法入门
1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 # //shell脚本注释符号 2.变量和使用 HOME= ...
- (转)Linux bash shell脚本语法入门
http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...
- Bash shell编程的语法知识点(1)
Bash shell脚本编程知识点如下(初学,不全,欢迎讨论补充): shell简介 脚本的简单介绍 变量和引用 算术运算 交互式编程 选择判断 条件测试 循环 函数 shell简介 shell是一种 ...
- bash shell最基本的语法
1 shell语句的基本构成 shell每个基本的构成元素之间都相隔一个空格. 比如[ -e file ],[.-e.file.]这四个基本元素之间都相隔了一个空格. 同样的道理[ ! -e file ...
- bash shell命令(2)
在上篇<bash shell命令(1)>中,介绍了几种简单的linux shell命令,今天继续介绍bash shell命令 本文地址:http://www.cnblogs.com/arc ...
- Shell函数语法
Shell函数语法 定义函数: function 函数名(){ 指令... } 调用函数,方法1: 函数名 调用函数,方法2: 函数名 参数一 参数二 return在函数里面使用会跳出函数并 ...
- Linux Bash Shell 快速入门
BASH 的基本语法 最简单的例子 —— Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1 ...
- 第四章:更多的bash shell命令
第四章:更多的bash shell命令 监测程序 ps (其他ps内容见#1 ) Unix风格的ps命令参数 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程( ...
- shell脚本学习之Bash shell 里各种括号的用法
今天在 SegmentFault 上看到又有人问起关于Shell里各种括号的问题.对于很多玩Shell的人,括号是个很尴尬的问题,用起来没问题,说起来不明白,我在这里总结一下Bash Shell几种括 ...
随机推荐
- 机器学习基石:06 Theory of Generalization
若H的断点为k,即k个数据点不能被H给shatter,那么k+1个数据点也不能被H给shatter,即k+1也是H的断点. 如果给定的样本数N是大于等于k的,易得mH(N)<2N,且随着N的增大 ...
- sprintf格式化字符串带来的注入隐患
原文链接:https://paper.seebug.org/386/ 摘要点关键知识点 <?php $input = addslashes("%1$' and 1=1#"); ...
- [NOI2016]循环之美
Description 牛牛是一个热爱算法设计的高中生.在他设计的算法中,常常会使用带小数的数进行计算.牛牛认为,如果在 k 进制下,一个数的小数部分是纯循环的,那么它就是美的.现在,牛牛想知道:对 ...
- ●POJ 3348 Cows
题链: http://poj.org/problem?id=3348 题解: 计算几何,凸包,多边形面积 好吧,就是个裸题,没什么可讲的. 代码: #include<cmath> #inc ...
- bzoj 2440 (莫比乌斯函数)
bzoj 2440 完全平方数 题意:找出第k个不是完全平方数的正整数倍的数. 例如 4 9 16 25 36什么的 通过容斥原理,我们减去所有完全数 4有n/4个,但是36这种会被重复减去, ...
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 操作符重载和隐式类型转换
C#中,某些类型会定义隐式类型转换和操作符重载.Unity中,有些对象也定义了隐式类型转换和操作符重载.典型情况有:UnityEngine.Object.UnityEngine.Object的销毁是调 ...
- 用HTTP状态码实现提交表单后刷新页面不重复提交
正常情况下,表单提交后如果用户刷新页面会重复提交表单,有些情况下我们不希望表单重复提交,利用HTTP协议中的307状态码重定向页面可以实现这个目的.实例如下: 表单页面代码: <form act ...
- 如何使用GitHub
进行版本控制时,我们可以直接通过网页来进行版本控制,也可以下载git客户端.值得注意的是,git的客户端有很多种,具体的可以参考:https://git-scm.com/downloads/guis/ ...
- 解决com.fasterxml.jackson.databind.JsonMappingException: No suitable
原因:直接翻译,json格式,不匹配. 这原因坑爹啊,因为json格式不正确算一种原因. 还有一种就是接收的bean没有getter,setter方法. 最坑的一种就是数据无法被反序列化,list,m ...
- 函数的形参和实参之arguments对象
当函数调用函数时候传入的实参比函数声明时候制定的形参要少时候,剩余的形参就设置成了undefined.例如 function getPropertyNames(o,/*optional*/a){ va ...