天天看點

shell之數組的使用

數組 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[@]}

3) 擷取數組的長度 

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

4

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

編寫腳本,找出數組中的最大數 

#!/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

本文轉自 北冥有大魚  51CTO部落格,原文連結:http://blog.51cto.com/lyw168/1957422,如需轉載請自行聯系原作者