良许Linux教程网 干货合集 Linux shell中for循环详解

Linux shell中for循环详解

在Linux shell 中for是常用的循环结构,其主要作用就是循环列表中的元素赋值给变量,每次赋值便执行一次循环,done就标志着一个循环的结束。下面良许教程网为大家详细讲解一下for循环的具体使用方法。

Linux shell中for循环详解

列表for循环语句用于将一组命令执行已知的次数,语句基本格式如下

 
 for variable in (list)
 do
     command
     command
     ...
 done

其中,do 和 done之间的命令成为循环体,执行次数和list列表中常数或字符串的个数相同。当执行for循环时,首先将in 后 list 列表的第一个常数或字符串赋给循环变量,然后执行循环体;接着将list 列表中的第二个常数或字符串赋值给循环变量,再次执行循环体。这个过程将一直持续到list 列表中无其它常数或字符串,然后执行done命令后的命令序列。

ex1,列表for循环中list 列表为常数的情况

 #!/bin/bash
 
 #使用列表for循环显示5次欢迎操作
 for variable  in 1 2 3 4 5
 do
     echo "Hello, welcome $variable  times "
 done

这种示例的循环经常用于计数,范围被限定在1~5之间。如下是脚本执行结果,由于in 后面列表列出了5个参数,可以看出脚本执行5次欢迎操作。

 [zhangqi@localhost shellscript]$ sh for_ex1.sh
 Hello, welcome 1  times
 Hello, welcome 2  times
 Hello, welcome 3  times
 Hello, welcome 4  times
 Hello, welcome 5  times
 [zhangqi@localhost shellscript]$

Linux shell中支持列表for 循环中使用略写的计数方式,我们将脚本略作改进

ex2,列表为略写形式

 #!/bin/bash
 
 #使用列表for循环显示5次欢迎操作
 for variable  in {1..5}
 do
     echo "Hello, welcome $variable  times "
 done

执行后,结果同脚本1相同

 [zhangqi@localhost shellscript]$ sh for_ex2.sh
 Hello, welcome 1  times
 Hello, welcome 2  times
 Hello, welcome 3  times
 Hello, welcome 4  times
 Hello, welcome 5  times
 [zhangqi@localhost shellscript]$

上面示例种,我们将1~5进行略写,使其可以正常的与示例1输出相同的结果

ex3,列表为简写形式

 #!/bin/bash
 
 #使用列表for循环显示5次欢迎操作
 for variable  in $(seq 1 5)
 do
     echo "Hello, welcome $variable  times "
 done

seq 命令是Linux预设的外部命令,一般用于一堆数字的简化写法,可以参考linux常用命令之seq

执行后,结果同上面相同,就不重复贴出来了。

ex4,按步数跳跃方式实现列表

 #!/bin/bash
 
 #使用列表for循环显示5次欢迎操作
 for variable  in {1..5..2}
 do
     echo "Hello, welcome $variable  times "
 done

in {1..5..2}  实现1~5之内的数字,按照步数2进行跳跃

运行下,看下结果

 [zhangqi@localhost shellscript]$ sh for_ex4.sh
 Hello, welcome 1  times
 Hello, welcome 3  times
 Hello, welcome 5  times
 [zhangqi@localhost shellscript]$

ex5、跳跃方式用seq表达

 [zhangqi@localhost shellscript]$ cat for_ex5.sh
 #!/bin/bash
 
 #使用列表for循环显示5次欢迎操作
 for variable  in $(seq 1 2 5)
 do
     echo "Hello, welcome $variable  times "
 done
 
 [zhangqi@localhost shellscript]$ sh for_ex5.sh
 Hello, welcome 1  times
 Hello, welcome 3  times
 Hello, welcome 5  times
 [zhangqi@localhost shellscript]$

ex6、用字符串表示列表

 [zhangqi@localhost shellscript]$ cat for_ex6.sh
 #!/bin/bash
 
 #使用列表for循环显示周一到周日对应的英文
 for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
 do
     echo "$day"
 done
 
 [zhangqi@localhost shellscript]$ sh for_ex6.sh
 Monday
 Tuesday
 Wednesday
 Thursday
 Friday
 Saturday
 Sunday
 [zhangqi@localhost shellscript]$

ex7、使用命令表示列表

 [zhangqi@localhost shellscript]$ cat for_ex7.sh
 #!/bin/bash
 
 #使用命令打印数组
 for variable  in `ls /`
 do
     echo "Every directory is $variable "
 done
 
 [zhangqi@localhost shellscript]$ sh for_ex7.sh
 Every directory is bin
 Every directory is boot
 Every directory is dev
 Every directory is etc
 Every directory is home
 Every directory is lib
 Every directory is lost+found
 Every directory is media
 Every directory is mnt
 Every directory is opt
 Every directory is proc
 Every directory is root
 Every directory is sbin
 Every directory is selinux
 Every directory is srv
 Every directory is sys
 Every directory is tmp
 Every directory is usr
 Every directory is var
 [zhangqi@localhost shellscript]$

这里的命令格式可以使用 $( command) 或 command,效果相同,这里就不再做展示了。

ex8、通过脚本传参实现里列表

 [zhangqi@localhost shellscript]$ cat for_ex8.sh
 #!/bin/bash
 
 echo "number of arguments is $#"
 
 echo "What you input is :"
 
 #使用命令打印数组
 for argument  in "$*"
 do
     echo "$argument "
 done
 
 [zhangqi@localhost shellscript]$ sh for_ex8.sh 1 hello shell
 number of arguments is 3
 What you input is :
 1 hello shell
 [zhangqi@localhost shellscript]$

可以看出,参数列表可以是数字,也可以是字符串,但是输入是以空格进行分隔的,如果存在空格,脚本执行时会认为存在另一个参数。

至此关于Linux shell中for循环的具体使用方法分享结束,大家有任何问题都可以通过下方评论区将问题提交给我们。

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!

137e00002230ad9f26e78-265x300

本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部