bash shell while语法
来源:互联网 发布:修改相片尺寸软件 编辑:程序博客网 时间:2024/05/21 03:57
在编写脚本时,一定要注意空格
基本语法:
while [ condition ]do command1 command2 command3done
condition为true时命令1到命令3将会一直执行,知道条件为false ,例如:
#!/bin/bashx=1while [ $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/bashcounter=$1factorial=1while [ $counter -gt 0 ]do factorial=$(( $factorial * $counter )) counter=$(( $counter - 1 ))doneecho $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/bashFILE=$1# read $FILE using the file descriptorsexec 3<&0exec 0<$FILEwhile read linedo# use $line variable to process lineecho $linedoneexec 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: optiondo 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;; esacdone.........
How do I use while as infinite loops?
Infinite for while can be created with empty expressions, such as:
#!/bin/bashwhile :doecho "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) thenbreak #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 :doread -p "Enter two numnbers ( - 1 to quit ) : " a bif [ $a -eq -1 ]thenbreakfians=$(( a + b ))echo $ansdone
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) thencontinue #Go to next iteration of I in the loop and skip statements3 fi statements3done
while [ condition ]do statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any. statements2 if (disaster-condition) thenbreak #Abandon the while lopp. fi statements3 #While good and, no disaster-condition.done
- bash shell while语法
- bash shell while语法
- shell语法while
- Shell bash 脚本语法
- bash shell语法复习
- Shell bash 脚本语法
- Shell bash 脚本语法
- Shell bash 脚本语法
- Shell中while基础语法
- shell bash的基本语法
- bash shell 基础(语法)
- shell bash 脚本基本语法
- bash shell 基础(语法)
- Linux bash shell脚本语法入门
- bash上shell编程的诡异语法
- Linux bash shell脚本语法入门
- Linux bash shell环境变量以及语法规范
- bash手册翻译#3.1 shell语法
- 各种网站汇总
- SSH框架mvc三层架构
- JAVA架构师葵花宝典
- 微信支付的大坑-1及集成过程
- laravel框架调用Guzzle
- bash shell while语法
- DebuggerStepThrough属性
- 【知识点】Android Studio之Gradle的几个知识点
- ucosIII 共享资源(信号量、互斥信号量)
- Android Studio 2.2 来啦
- 用python实现文本分类
- 项目启动报错.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
- SQL Server之存储过程基础知识
- map与unordered_map