完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在编写脚本时,一定要注意空格 基本语法: 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 $factorialTo run just type: 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 line echo $linedoneexec 0<&3You 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 :do echo "infinite loops [ hit CTRL+C to stop]"doneConditional while loop exit with break statementYou 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.doneIn 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 $ansdoneEarly continuation with the continue statementTo 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 statements3donewhile [ 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 |
|
相关推荐 |
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 22:27 , Processed in 0.669357 second(s), Total 54, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号