温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux for循环怎么理解

发布时间:2022-01-21 11:02:43 阅读:226 作者:柒染 栏目:开发技术
Linux服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这期内容当中小编将会给大家带来有关linux for循环怎么理解,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

for循环是Linux shell 中最常用的结构。for 循环有三种结构:一种结构是列表for循环;第二种结构是不带列表for循环;第三种结构是类C风格的for循环。

for var in listdocommandsdone#!/bin/bashfor test in alabama alaska arizona arkansas california coloradodoecho the next state is $testdone>the next state is alabamathe next state is alaskathe next state is arizonathe next state is arkansasthe next state is californiathe next state is colorado#!/bin/bashfor test in alabama alaska arizona arkansas california coloradodoecho "the next state is $test"doneecho "The las state we visited was $test"test=connecticutecho "wait, now we're visiting $test">the next state is alabamathe next state is alaskathe next state is arizonathe next state is arkansasthe next state is californiathe next state is coloradoThe las state we visited was coloradowait, now we're visiting connecticut#!/bin/bashfor test in I don't know if this'll workdoecho "word:$test"done>word:Iword:dont know if thisllword:work#!/bin/bashfor test in nevada "New Hampshire" "New Mexico" "New York"doecho "Now going to $test"done>Now going to nevadaNow going to New HampshireNow going to New MexicoNow going to New York#!/bin/bashlist="Alabama ALaska Arizona Arkansas Colorado"list=$list" Connecticut"for state in $listdoecho "Have you ever visited $state?"done>Have you ever visited Alabama?Have you ever visited ALaska?Have you ever visited Arizona?Have you ever visited Arkansas?Have you ever visited Colorado?Have you ever visited Connecticut?#!/bin/bashfile="states"for state in `cat $file`doecho "Visit beautiful  $state"done#cat states>AlabamaAlaskadddfacdfsasecvcvssdfqzcvcddddwqbnnnqqojfaso>Visit beautiful  AlabamaVisit beautiful  AlaskaVisit beautiful  dddfacVisit beautiful  dfsaseVisit beautiful  cvcvssVisit beautiful  dfqzcvcVisit beautiful  ddddwqVisit beautiful  bnnnVisit beautiful  qqojfaso #!/bin/bashIFS=$'\n'file="states"for state in `cat $file`doecho "visit beautiful $state"done>#cat states>A           labamaAlaskadddfacdfsasecvcvssdfqzcvcddddwqbnnnqqojfaso>visit beautiful A           labamavisit beautiful Alaskavisit beautiful dddfacvisit beautiful dfsasevisit beautiful cvcvssvisit beautiful dfqzcvcvisit beautiful ddddwqvisit beautiful bnnnvisit beautiful qqojfaso在处理长脚本时,可能在一个地方需要修改IFS的值,然后忘掉它并在脚本中其他地方以为还是默认的值。一个可参考的简单实践:IFS.OLD=$IFSIFS=$'\n'IFS=$IFS.OLD#!/bin/bashfor file in /root/*doif  [ -d "$file" ]thenecho " $file is a directory "elif [ -f "$file" ]thenecho "$file is a file"fidone#!/bin/bashfor file in /root/.b* /home/rich/badtestdoif [ -d "$file" ]thenecho "$file is a directory"elif [ -f "$file" ]thenecho "$file is a file"elseecho "$file doesn't exist"fidone类C语言风格#!/bin/bashfor (( i=1; idoecho "The next nuber is $i"done>The next nuber is 1The next nuber is 2The next nuber is 3The next nuber is 4The next nuber is 5The next nuber is 6The next nuber is 7The next nuber is 8The next nuber is 9The next nuber is 10#!/bin/bashfor (( a=1, b=10; a doecho "$a - $b"done>1 - 102 - 93 - 84 - 75 - 66 - 57 - 48 - 39 - 210 - 111 - 012 - -1

上述就是小编为大家分享的linux for循环怎么理解了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×