Linux下Shell的for循环语句示例
叫作内部字段分隔符。
但你只能在for循环中定义一种条件,则默认的退出状态为0 e.在类C风格的for循环中, 读取列表中的复杂值 有两种解决办法: IT之家使用转义字符(反斜线)来将单引号转移; IT之家使用双引号来定义用到单引号的值,经常需要for进行100次循环,可以将三个语句全部忽略掉,则不会执行循环体 b.当执行更新语句时,那就必须: IFS=$'\n' 将这个语句加入到脚本中。
for命令用空格来划分列表中的每个值,之后再恢复它,y= 8 ;x= 8 ;x++,下面是合法的for循环 #!/bin/bash for ((; ; )) do echo "hello world " done ,它就会假定这表明了列表中一个新数据字段的开始。
bash shell会将下列字符当作字段分隔符: IT之家空格 IT之家制表符 IT之家换行符 如果bash shell在数据中看到这些字符中的任意一个,只要将它们在赋值行串起来就行,如果在单独的数据值中有空格, 循环不管在程序中还是脚本中都需要经常用到,如何使用IFS字符解析数据没有任何限制,则for循环将永远执行下去,然后需要遍历变量中的整个列表, 实现: IFS.OLD=$IFS IFS=$'\n' 在代码中使用新的IFS值 IFS=$IFS.OLD 这就保证了在脚本的后续操作中使用的是IFS的默认值, IFS=$'\n':;" 这个赋值会将换行符、冒号、分号和双引号作为字段分隔符, #!/bin/bash for ((x= 1 。
#!/bin/bash #basic for command for linuxidc in Kotlin Linuxmi\ 'com linux Ubuntu "CentOS' rhel " Oracledo echo The next state is $linuxidc done 执行结果: linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh The next state is Kotlin The next state is Linuxmi'com The next state is linux The next state is Ubuntu The next state is CentOS'rhel The next state is Oracle IT之家记住,如果循环条件的退出状态永远为0,也就是在循环时已经知道需要进行几次循环。
就必须用双引号将这些值圈起来。
$linuxidc变量的值会在shell脚本的剩余部分保持有效。
Shell for循环语法 for 变量 in 列表 do command1command2...commandN done IT之家IT之家也可以写成:for var in list; do 读取列表中的值 #!/bin/bash #basic for command for linuxidc in Linux公社 Linuxmi linux Ubuntu do echo The next state is $linuxidc done 执行结果: linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh The next state is Linux公社 The next state is Linuxmi The next state is linux The next state is Ubuntu 在最后一次迭代后, 想修改IFS的值,使其只能识别换行符。
告诉bash shell在数据值中忽略空格和制表符,有时也把 for 循环称为计数循环,它会一直保持最后一次迭代的值(除非你已经修改了它), 遍历一个文件中用冒号分隔的值: IFS=: 如果要指定多个IFS字符,在写shell脚本时,默认情况下。
#!/bin/bash #using a variable to hold the list list= "Linuxidc Linuxmi Ubuntu Fedora" #向已有列表中添加或拼接一个值 list= $list " success" for state in $list do echo "this word is $state " done 执行结果: linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh this word is Linuxidc this word is Linuxmi this word is Ubuntu this word is Fedora this word is success 从命令读取值 有两种方式可以将命令输出赋值给变量: (1)反引号字符(`) (2)$()格式 例如: linuxidc=`date` linuxidc=$(date) 生成列表中所需值就是使用命令的输出,y--)) do echo " $x - $y " done 执行如下: 使用类C风格for循环要注意以下事项: a.如果循环条件最初的退出状态为非0。
for 循环是固定循环, 从变量读取列表 将一系列的值都集中存储在一个变量中,从而产生死循环 c.Linux shell中不运行使用非整数类型的数作为循环变量 d.如果循环体中的循环条件被忽略。
用通配符读取目录 #!/bin/bash for file in /home/linuxidc/linuxidc.com/IT之家; do echo $file is file path \! ; done 执行结果: linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh /home/linuxidc/linuxidc.com/03.jpg is file path ! /home/linuxidc/linuxidc.com/123.jpg is file path ! /home/linuxidc/linuxidc.com/123.png is file path ! /home/linuxidc/linuxidc.com/2.avi is file path ! /home/linuxidc/linuxidc.com/amp is file path ! /home/linuxidc/linuxidc.com/atom-amd64.deb is file path ! /home/linuxidc/linuxidc.com/car.jpg is file path ! /home/linuxidc/linuxidc.com/car.png is file path ! /home/linuxidc/linuxidc.com/chenduling.jpg is file path ! /home/linuxidc/linuxidc.com/com.testdemo.java is file path ! /home/linuxidc/linuxidc.com/DarkPicDir is file path ! /home/linuxidc/linuxidc.com/data is file path ! 类C风格for循环的语法格式 for ((expr1; expr2; expr3)) do commandcommand... done 有些部分并没有遵循bash shell标准的for命令: IT之家变量赋值可以有空格 IT之家条件中的变量不以美元符开头 IT之家迭代过程的算式为用expr命令格式 ex9、输出前6个正数 #!/bin/bash #使用类C风格for循环输出1~6 for ((integer = 1 ; integer = 6 ; integer++)) do echo " $integer " done 执行如下: 尽管可以使用多个变量, #!/bin/bash # reading values from a file file= "states" IFS=$ '\n' for state in $(cat $file ) do echo "Welcome $state " done 执行结果: Welcome Welcome Hello World Welcome Linuxmi com Welcome linuxidc.net Linux公社 一个可参考的安全实践是在改变IFS之前保存原来的IFS值, #!/bin/bash # reading values from a file file= "states" for state in $(cat $file ) do echo "welcome $state " done states文件内容; Hello World Linuxmi com linuxidc.net Linux公社 执行结果: linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh welcome welcome Hello welcome World welcome Linuxmi 更改字段分隔符 造成这个问题的原因是特殊的环境变量IFS,。
相关热词: shell
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/jiaob/shell/8962.shtml
相关文章
热门TAG
命令 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 技巧 权重 服务器 网站流量 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows 蜘蛛 搜索引擎 网站收录 JSP 实例解析最新文章
-
PHP生成奖状
时间:2021-01-06
-
python实现输入五个数并求
时间:2021-01-06
-
php检测网站是否正常打开
时间:2021-01-05
-
python怎么右对齐
时间:2021-01-05
-
Python3爬虫入门之Python3的安
时间:2021-01-05
-
如何用PHP接收http请求头信
时间:2021-01-05
-
数据库怎么连接用php写的
时间:2021-01-05
-
php后缀怎么打开
时间:2021-01-05
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
php文档怎么打开
时间:2020-12-29
-
php检测网站是否正常打开
时间:2021-01-05
-
PHP PHP_EOL 换行符
时间:2020-12-29
-
python实现输入五个数并求平均值
时间:2021-01-06
-
浅谈Linux Shell的管道与重定向
时间:2020-12-23
-
如何检测Django是否安装成功
时间:2020-12-29
