From :http://blog.csdn.net/snrqtdhuqf/article/details/7242309?
在shell中,數組變量的賦值有兩種方法:
(1) name = (value1 ... valuen)此時下標從0開始
(2) name[index] = value
?example:
- #!/bin/sh??
- #arrayTest??
- name=(yunix?yhx?yfj)??
- echo?"array?is:${name[@]}"??
- echo?"array?length?is:${#name[*]}"??
- echo?${name[1]}??
- name[1]=yang??
- echo?${name[1]}??
- read?-a?name??
- echo?${name[1]}??
- echo?"loop?the?array"??
- len=${#name[*]}??
- i=0??
- while?[?$i?-lt?$len?]??
- do??
- echo?${name[$i]}??
- let?i++??
- done??
result:
array is:yunix yhx yfj
array length is:3
yhx
yang
a b c d e
b
loop the array
a
b
c
d
e
?
下面的是關于數組的輸出實例
example:
#!/bin/sh ?
- #arrayLoopOut??
- read?-a?array??
- len=${#array[*]}??
- echo?"array's?length?is?$len"??
- echo?"use?while?out?the?array:"??
- i=0??
- while?[?$i?-lt?$len?]??
- do??
- ????????echo?-n?"${array[$i]}"??
- let?i++??
- done??
- echo??
- echo?"use?for?out?the?array:"??
- for?((j=0;j<"$len";j=j+1))??
- do??
- ????????echo?-n?${array[$j]}??
- done??
- echo??
- echo?"use?for?in?out?the?array:"??
- for?value?in?${array[*]}??
- do??
- echo?-n?$value??
- done??
result:
a b c d e f g
array's length is 7
use while out the array:
abcdefg
use for out the array:
abcdefg
use for in out the array:
abcdefg
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
