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几种括 ...
随机推荐
- Pycharm节能模式
如题,开启节能模式代码不会自动补全.
- [SDOI 2013]森林
Description 题库链接 给你 \(n\) 个节点,初始 \(m\) 条边, \(t\) 组操作.让你支持: 询问树上路径点权 \(K\) 小: 支持加边操作. 强制在线,所有状态保证是一个树 ...
- [Wc2010]重建计划
Description Input 第一行包含一个正整数N,表示X国的城市个数. 第二行包含两个正整数L和U,表示政策要求的第一期重建方案中修建道路数的上下限 接下来的N-1行描述重建小组的原有方案, ...
- 洛谷P3275 [SCOI2011]糖果
差分约束大坑题 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
- APIO 2015
老师让我们打这套题练练手.感觉这套题还是挺有意思的,比国内某些比赛不知道高到哪里去.最后我拿了284/300,貌似比赛是IOI赛制啊,强行被当成OI赛制做了,不然我T3可能还能多骗点. T1.scul ...
- NOIP2014-5-24模拟赛
Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...
- python中读取文件数据时要注意文件路径
我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...
- vim的基本介绍
vim四种模式: 1.正常模式(Normal-mode) 当我们打开vim时就会进入到正常模式,这个时候是不能编辑的,一般用于浏览文件,包括复制,删除一些文本操作.我们可以通过键盘的上下左右来进行光标 ...
- YOLO2:实时目标检测视频教程,视频演示, Android Demo ,开源教学项目,论文。
实时目标检测和分类 GIF 图: 视频截图: 论文: https://arxiv.org/pdf/1506.02640.pdf https://arxiv.org/pdf/1612.08242.pdf ...
- Linux学习之CentOS(十五)----磁盘管理之 启动挂载(转)
启动挂载 /etc/fstab 及 /etc/mtab 刚刚上面说了许多,那么可不可以在启动的时候就将我要的文件系统都挂好呢?这样我就不需要每次进入 Linux 系统都还要在挂载一次呀!当然可以啰!那 ...