您的位置:首页 > 运维架构 > Shell

shell之数组的使用

2017-08-18 17:32 357 查看
数组 Array

一段连续的内存空间

1) 定义数组

[root@shellscript shell]# aa[0]=martin
[root@shellscript shell]# aa[1]=jerry
[root@shellscript shell]# aa[2]=mike
[root@shellscript shell]# aa[10]=alice

[root@shellscript shell]# bb=(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4)

2) 调用数组的值

[root@shellscript shell]# echo ${bb[2]}
192.168.1.3

[root@shellscript shell]# echo ${bb[1]}
192.168.1.2

[root@shellscript shell]# echo ${bb[*]}
192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4

[root@shellscript shell]# echo ${bb[@]}
192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4

3) 获取数组的长度

[root@shellscript shell]# echo ${#bb[*]}
4
[root@shellscript shell]# echo ${#bb[@]}
4

编写脚本,找出数组中的最大数

#!/bin/bash
#

aa=(14 543 64 235 76 345 765)

max=${aa[0]}

for i in `seq 6`; do
if [ $max -lt ${aa[$i]} ]; then
max=${aa[$i]}
fi
done

echo $max
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell之数组