1. 程式人生 > >shell之數組的使用

shell之數組的使用

shell之數組

數組 Array

一段連續的內存空間

1) 定義數組


[[email protected] shell]# aa[0]=martin

[[email protected] shell]# aa[1]=jerry

[[email protected] shell]# aa[2]=mike

[[email protected] shell]# aa[10]=alice


[[email protected] shell]# bb=(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4)

2) 調用數組的值


[[email protected] shell]# echo ${bb[2]}

192.168.1.3


[[email protected] shell]# echo ${bb[1]}

192.168.1.2


[[email protected] shell]# echo ${bb[*]}

192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4


[[email protected] shell]# echo ${bb[@]}

192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4

3) 獲取數組的長度


[[email protected] shell]# echo ${#bb[*]}

4

[[email protected] 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


本文出自 “lyw666” 博客,請務必保留此出處http://lyw666.blog.51cto.com/12823216/1957422

shell之數組的使用