Linux for循环之列表for循环详解

张开发
2026/4/3 11:22:58 15 分钟阅读
Linux for循环之列表for循环详解
for循环是Linux shell 中最常用的结构。for 循环有三种结构一种结构是列表for循环第二种结构是不带列表for循环第三种结构是类C风格的for循环本篇博文重点看列表for循环列表for循环大的格式固定在列表构成上分多种情景如数字列表、字符串列表、命令列表、脚本传参列表等下面一一来看。列表for循环语句用于将一组命令执行已知的次数语句基本格式如下123456forvariablein(list)docommandcommand...doneex1列表for循环中list 列表为常数的情况1234567#!/bin/bash#使用列表for循环显示5次欢迎操作forvariablein1 2 3 4 5doechoHello, welcome $variable times done1234567[zhangqilocalhost shellscript]$ sh for_ex1.shHello, welcome 1timesHello, welcome 2timesHello, welcome 3timesHello, welcome 4timesHello, welcome 5times[zhangqilocalhost shellscript]$ex2列表为略写形式1234567#!/bin/bash#使用列表for循环显示5次欢迎操作forvariablein{1..5}doechoHello, welcome $variable times done1234567[zhangqilocalhost shellscript]$ sh for_ex2.shHello, welcome 1timesHello, welcome 2timesHello, welcome 3timesHello, welcome 4timesHello, welcome 5times[zhangqilocalhost shellscript]$上面示例种我们将1~5进行略写使其可以正常的与示例1输出相同的结果ex3列表为简写形式1234567#!/bin/bash#使用列表for循环显示5次欢迎操作forvariablein$(seq1 5)doechoHello, welcome $variable times doneseq 命令是Linux预设的外部命令一般用于一堆数字的简化写法可以参考linux常用命令之seq。执行后结果同上面相同就不重复贴出来了。ex4按步数跳跃方式实现列表1234567#!/bin/bash#使用列表for循环显示5次欢迎操作forvariablein{1..5..2}doechoHello, welcome $variable times done运行下看下结果12345[zhangqilocalhost shellscript]$ sh for_ex4.shHello, welcome 1timesHello, welcome 3timesHello, welcome 5times[zhangqilocalhost shellscript]$ex5跳跃方式用seq表达1234567891011121314[zhangqilocalhost shellscript]$catfor_ex5.sh#!/bin/bash#使用列表for循环显示5次欢迎操作forvariablein$(seq1 2 5)doechoHello, welcome $variable times done[zhangqilocalhost shellscript]$ sh for_ex5.shHello, welcome 1timesHello, welcome 3timesHello, welcome 5times[zhangqilocalhost shellscript]$ex6用字符串表示列表123456789101112131415161718[zhangqilocalhost shellscript]$catfor_ex6.sh#!/bin/bash#使用列表for循环显示周一到周日对应的英文fordayinMonday Tuesday Wednesday Thursday Friday Saturday Sundaydoecho$daydone[zhangqilocalhost shellscript]$ sh for_ex6.shMondayTuesdayWednesdayThursdayFridaySaturdaySunday[zhangqilocalhost shellscript]$123456789101112131415161718192021222324252627282930[zhangqilocalhost shellscript]$catfor_ex7.sh#!/bin/bash#使用命令打印数组forvariableinls/doechoEvery directory is $variable done[zhangqilocalhost shellscript]$ sh for_ex7.shEvery directory is binEvery directory is bootEvery directory is devEvery directory is etcEvery directory is homeEvery directory is libEvery directory is lostfoundEvery directory is mediaEvery directory is mntEvery directory is optEvery directory is procEvery directory is rootEvery directory is sbinEvery directory is selinuxEvery directory is srvEvery directory is sysEvery directory is tmpEvery directory is usrEvery directory is var[zhangqilocalhost shellscript]$这里的命令格式可以使用 $( command) 或 command效果相同这里就不再做展示了。ex8通过脚本传参实现里列表123456789101112131415161718[zhangqilocalhost shellscript]$catfor_ex8.sh#!/bin/bashechonumber of arguments is $#echoWhat you input is :#使用命令打印数组forargumentin$*doecho$argument done[zhangqilocalhost shellscript]$ sh for_ex8.sh 1 hello shellnumber of arguments is 3What you input is :1 hello shell[zhangqilocalhost shellscript]$总结以上为个人经验希望能给大家一个参考

更多文章