linux shell for 循环变量,shell for循环总结

1 shell for循环语法

for 变量 in 列表

do

command1

command2

...

commandN

done

1.1 读取列表中的值

#!/bin/bash

for test in apple boy cat dog

do

echo The next state is $test

done

结果:

The next state is apple

The next state is boy

The next state is cat

The next state is dog

1.2 读取列表中的复杂值

有两种解决办法:

#使用转义字符(反斜线)来将单引号转移;

#使用双引号来定义用到单引号的值。

#!/bin/bash

for test in I don\'t think if "this'll" work

do

echo The next state is $test

done

结果是:

The next state is I

The next state is don't

The next state is think

The next state is if

The next state is this'll

The next state is work

*记住,for命令用空格来划分列表中的每个值。如果在单独的数据值中有空格,就必须用双引号将这些值圈起来。

1.3 从变量读取列表

将一系列的值都集中存储在一个变量中,然后需要遍历变量中的整个列表。

#!/bin/bash

list="helllo world"

#向已有列表中添加或拼接一个值

list=$list" ok"

for state in $list

do

echo "this word  is $state"

done

结果是:

this word is helllo

this word  is world

this word  is ok

1.4 从命令读取值

有两种方式可以将命令输出赋值给变量:

(1)反引号字符(``)

(2)$()格式

如:

for i in $(who)

do

echo "visit beautiful $i"

done

输出结果:

visit beautiful etldev

visit beautiful pts/0

visit beautiful 2019-05-15

visit beautiful 09:31

visit beautiful (112.64.161.227)

visit beautiful root

visit beautiful pts/1

visit beautiful 2019-05-09

visit beautiful 07:41

visit beautiful (10.1.1.62)

visit beautiful etldev

visit beautiful pts/3

visit beautiful 2019-05-15

visit beautiful 09:34

visit beautiful (112.64.161.227)

visit beautiful etldev

visit beautiful pts/4

visit beautiful 2019-05-15

visit beautiful 10:49

visit beautiful (112.64.161.227)

*who默认输出当前登录的所有用户的信息如下所示

etldev pts/0        2019-05-15 09:31 (112.64.161.227)

root    pts/1        2019-05-09 07:41 (10.1.1.62)

etldev  pts/3        2019-05-15 09:34 (112.64.161.227)

etldev  pts/4        2019-05-15 10:49 (112.64.161.227)

1.5 更改字段分隔符

造成这个问题的原因是特殊的环境变量IFS,叫作内部字段分隔符。默认情况下,bash shell会将下列字符当作字段分隔符:

*空格

*制表符

*换行符

如果bash shell在数据中看到这些字符中的任意一个,它就会假定这表明了列表中一个新数据字段的开始。

想修改IFS的值,使其只能识别换行符,那就必须:

IFS=$'\n'

将这个语句加入到脚本中,告诉bash shell在数据值中忽略空格和制表符。

一个可参考的安全实践是在改变IFS之前保存原来的IFS值,之后再恢复它。

实现:

IFS.OLD=$IFS

IFS=$'\n'

IFS=$IFS.OLD

这就保证了在脚本的后续操作中使用的是IFS的默认值。

遍历一个文件中用冒号分隔的值:

IFS=:

如果要指定多个IFS字符,只要将它们在赋值行串起来就行。

IFS=$'\n':;"

这个赋值会将换行符、冒号、分号和双引号作为字段分隔符。如何使用IFS字符解析数据没有任何限制。

1.6 用通配符读取目录

for file in /proc/*;

do

echo $file is file path \! ;

done

2 类C风格for循环的语法格式

for((expr1; expr2; expr3))

do

command

command

...

done

有些部分并没有遵循bash shell标准的for命令:

*变量赋值可以有空格

*条件中的变量不以美元符开头

*迭代过程的算式为用expr命令格式

#!/bin/bash

#使用类C风格for循环输出1~5

for ((integer = 1; integer <= 5; integer++))

do

echo "$integer"

done

结果:

1

2

3

4

5

使用类C风格for循环要注意以下事项:

a.如果循环条件最初的退出状态为非0,则不会执行循环体

b.当执行更新语句时,如果循环条件的退出状态永远为0,则for循环将永远执行下去,从而产生死循环

c.Linux shell中不运行使用非整数类型的数作为循环变量

d.如果循环体中的循环条件被忽略,则默认的退出状态为0

e.在类C风格的for循环中,可以将三个语句全部忽略掉,下面是合法的for循环

for((; ; ))

do

echo "hello world"

done

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值