Linux下Shell的for循环语句N种写法

网友投稿 797 2022-10-12

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

Linux下Shell的for循环语句N种写法

运维人员,不管是应用运维,还是数据库运维,系统运维人员,都会掌握一门编程语言,而shell脚本语言是运维人员最常用的,for循环又是shell脚本出现频率最高的,下面就介绍一下Shell的for循环语句N种写法。

循环输出50个数字

第一种写法

[root@localhost ~]# cat 1.sh #!/bin/bashfor ((i=1;i<=50;i++));doecho $idone

第二种写法

[root@localhost ~]# cat 2.sh #!/bin/bashfor i in $(seq 1 50)doecho $idone

第三种写法

[root@localhost ~]# cat 3.sh #!/bin/bashfor i in {1..50}doecho $idone

第四种写法

[root@localhost ~]# cat 4.sh#!/bin/bashawk 'BEGIN{for(i=1; i<=50; i++) print i}'

字符性循环

第一种写法

a.txt文件内容为1到50数字列表

[root@localhost ~]# cat a.txt12345...50[root@localhost ~]# cat 5.sh#!/bin/bashfor i in `cat a.txt`doecho $idone

第二种写法

[root@localhost ~]# cat 6.sh#!/bin/bashfor i in `ls`doecho $idone[root@localhost ~]# ./6.shsql.txt.gzsysbench-1.0.17-2.el7.x86_64.rpmtest.log

第三种写法

[root@localhost ~]# cat 7.sh#!/bin/bashlist_str="test1 test2 test3 test4"for i in $list_str;doecho $idone[root@localhost ~]# ./7.shtest1test2test3test4

第四种写法

[root@localhost ~]# cat 8.sh#!/bin/bashfor file in $(ls)doecho $filedone[root@localhost ~]# ./8.shsql.txt.gzsysbench-1.0.17-2.el7.x86_64.rpmtest.log

这个技能你get了吧

上一篇:服务器运维系列:阿里云服务器使用快照功能创建服务器备份
下一篇:Oracle Database 19c中的自动索引
相关文章

 发表评论

暂时没有评论,来抢沙发吧~