Linux Shell系列教程之(十)Shell for循环

来源:互联网 发布:androis中的js调试 编辑:程序博客网 时间:2024/05/16 19:28

本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程

基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shell for循环的用法。

Shell for循环

Shell for循环语法

Shell for循环的语法如下所示

for 变量 in 列表do    command1    command2    ...    commandNdone

列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的值依序放入指定的变量中,然后重复执行命令区域(在do和done 之间),直到所有元素取尽为止。

Shell for循环示例

接下来通过示例的方式给大家介绍下Shell for循环的用法。

for loop in one two tree fourdo    echo "I am : $loop"done

输出结果:

I am : oneI am : twoI am : treeI am : four

可以顺序输出字符串的单词,示例:

for str in I am linuxdaxuedo    echo $strdone

输出结果:

Iamlinuxdaxue

可以显示当前目录下的文件,示例:

for file in ./*do    echo $filedone

输出结果:

./a.sh./automake./automonitor./crzk./makedir./shell_start./sql./submit

Shell for循环命令是比较容易掌握的,今天就先给大家介绍到这里。更多Shell教程请看:Linux Shell系列教程


  • 版权声明:本站原创文章,于4个月前,由Linux大学(Linuxdaxue.com)发表,共 634字。
  • 转载请注明:Linux Shell系列教程之(十)Shell for循环 | Linux大学

0 0
原创粉丝点击