壳牌编程中为/,/直到循环命令

  

<强>

,,,,在壳牌编程中,有时我们需要重复执行一直命令直至达到某个特定的条件,bash shell中,提供了,命令,允许你创建一个遍历一系列值的循环,每次迭代都通过一个该系列中的值执行一组预定义的命令。

的基本格式:

,,,,的var列表

,,,,做

,,,,,,,,命令

,,,,做

,,,,在列表中,你提供了迭代中要用的一系列值。在每个迭代中,变量var包含列表中的当前值,第一个迭代会适用列表中的第一个值,第二个迭代使用第二个值,以此类推,直至列表中的所有值都过一遍。


<强>

(root@sh  shell) #, cat  for1.sh   # !/bin/bash   for  test  aaa 拷贝;bbb  ccc  ddd   做   ,,,,,,,echo 从而next  state  is  $测试   完成      (root@sh  shell) #, sh  for1.sh    从而next  state  is  aaa   从而next  state  is  bbb   从而next  state  is  ccc   从而next  state  is  ddd

测试美元变量的值会在shell脚本的最后一次迭代中一直保持有效,除非你修改了它

(root@sh  shell) #, cat  for1.sh    # !/bin/bash   for  test  aaa 拷贝;bbb  ccc  ddd   做   echo 从而next  state  is  $测试   完成   echo “, last  state  we  visited  was 美元测试”   测试=fff   echo “只等待你我们're  visiting 美元测试”         (root@sh  shell) #, sh  for1.sh    从而next  state  is  aaa   从而next  state  is  bbb   从而next  state  is  ccc   从而next  state  is  ddd   从而last  state  visited 我方表示歉意was  ddd   只等待你;我们're  visiting  fff


在shell脚本中,优势你会遇到难处理的数。下面是个给shell脚本程序员带来麻烦的经典例子:

(root@sh  shell) #, cat  for2.sh    # !/bin/bash   for  test 小姐:拷贝't 不知道if 这'll 工作   做   echo “词:$测试”   完成      (root@sh  shell) #, sh  for2.sh    词:我   词:dont 知道if  thisll   词:工作

解决办法:使用转义符或者双引号

(root@sh  shell) #, cat  for2.sh    # !/bin/bash   for  test 小姐:拷贝不\ ' t 知道if “这”,工作   做   echo “词:$测试”   完成      (root@sh  shell) #, sh  for2.sh    词:我   词:不   词:知道   词:如果   词:这   词:工作


记住:为循环假定每一个值都是用空格分割的,如果在单独的数字值中有空格,那么你必须使用双引号来将这些值圈起来。



<强>

(root@sh  shell) #, cat  for3.sh    # # # # # # # # # # # # # # # # # # # # # # # # # # # #   # !/bin/bash      列表=" aaa  bbb  ccc  ddd  eee”=$列表”,康涅狄格   for  state 美元拷贝列表      做   echo 才能;“Have 你;ever  visited 美元状态”   完成         (root@sh  shell) #, sh  for3.sh    Have 你;ever  visited  aaa   Have 你ever  visited  bbb   Have 你ever  visited  ccc   Have 你ever  visited  ddd   Have 你ever  visited  eee   康涅狄格州Have 你ever  visited 


(root@sh  shell) #, cat  for4.sh    # !/bin/bash   文件="/根/壳/国家”,,#如果是在当前不用绝对路径,文件=爸荨奔纯?      for  state  cat  $文件的拷贝   做   echo 才能“Visit  beautiful  $状态”   完成      (root@sh  shell) #, sh  for4.sh    Visit  beautiful 上海   Visit  beautiful 北京   Visit  beautiful 杭州   Visit  beautiful 南京   Visit  beautiful 广州      (root@sh  shell) #, cat  states    上海   北京   杭州   南京   广州

壳牌编程中为/,/直到循环命令