Shell BASICS (8)-cyclic statements

Source: Internet
Author: User

   国庆过后;感觉有点慵懒些了;接着上篇;我们继续来学习循环语句。

    一. for循环

        与其他编程语言类似,Shell支持for循环。

for循环一般格式为:for 变量 in 列表do    command1    command2    ...    commandNdone

   列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量

   例如,顺序输出当前列表中的数字

for01.sh$ cat for01.sh #!/bin/shfor i in 1 2 3 4 5do echo "this is $i"done$ ./for01.sh this is 1this is 2this is 3this is 4this is 5

 当然也可以向其他语言那样for ((i=1;i++<5));但是是要双括号;这个是与众不同。

#!/bin/shfor ((i=1;i<=5;i++))do echo "this is $i"done

 【注意】in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。如下:

$ cat for01.sh #!/bin/shfor ido echo "this is $i"done$ ./for01.sh 1 2 3 4 5  this is 1this is 2this is 3this is 4this is 5

 【note】对于列表;像上面一样;其实命令ls当前目录下的所有文件就是一个列表

 

   二.while 循环

while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件

#其格式为:while commanddo   Statement(s) to be executed if command is truedone

 命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。
以for循环的例子。

$ cat while01.sh #!/bin/shi=0while [ $i -lt 5 ]do let "i++" echo "this is $i"done$ ./while01.sh this is 1this is 2this is 3this is 4this is 5

 其实while循环用的最多是用来读文件。

#!/bin/bashcount=1    cat test | while read line        #cat 命令的输出作为read命令的输入,read读到的值放在line中do   echo "Line $count:$line"   count=$[ $count + 1 ]          done或者如下#!/bin/shcount=1while read linedo   echo "Line $count:$line"   count=$[ $count + 1 ]  done < test

 【注意】当然你用awk的话;那是相当简单;awk ‘{print "Line " NR " : " $0}‘ test
输出时要去除冒号域分隔符,可使用变量IFS。在改变它之前保存IFS的当前设置。然后在脚本执行完后恢复此设置。使用IFS可以将域分隔符改为冒号而不是空格或tab键

例如文件worker.txtLouise Conrad:Accounts:ACC8987Peter Jamas:Payroll:PR489Fred Terms:Customer:CUS012James Lenod:Accounts:ACC887Frank Pavely:Payroll:PR489while02.sh如下:#!/bin/sh#author: li0924#SAVEIFS=$IFSIFS=:while read name dept id do  echo -e "$name\t$dept\t$id" done < worker.txt#IFS=$SAVEIFS

 

   三.until循环

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反

until 循环格式为: until commanddo   Statement(s) to be executed until command is truedone

 command 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环

$ cat until01.sh #!/bin/shi=0until [ $i -gt 5 ]do let "i++" echo "this is $i"done

 一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用。详细介绍until就不必要了

 

   四. break和continue命令

1. break命令
break命令允许跳出所有循环(终止执行后面的所有循环)
2.continue命令
continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

break01.sh#!/bin/shfor ((i=1;i<=5;i++))do if [ $i == 2 ];then break else echo "this is $i" fidone

 至于continue命令演示;你就把break替换下;执行看下效果就行了。不解释。

shell基础(八)-循环语句

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.