shell指令碼-關係數組

來源:互聯網
上載者:User

標籤:


  1 #########################################################################     2 # File Name: muscian.sh  3 # Author: dty  4 # mail: [email protected]  5 # Created Time: Wed 05 Aug 2015 04:41:14 PM CST  6 #########################################################################  7 #!/bin/bash  8 declare -A beatles  9 beatles=( [singer]=john [bassist]=Paul [drummer]=ringo [guitarist]=george) 10 for muscian in  singer bassist drummer guitarist 11 do 12     echo "The ${muscian} is ${beatles[$muscian]}"//注意調用,調用的時候必須加上$符號在[]裡。。。。 13 done~

  1 #########################################################################     2 # File Name: instruments.sh  3 # Author: dty  4 # mail: [email protected]  5 # Created Time: Wed 05 Aug 2015 04:50:45 PM CST  6 #########################################################################  7 #!/bin/bash  8 declare -A beatles  9 beatles=( [singer]=john [bassist]=paul [drummer]=ringo [guitarist]=george ) 10 for instrument in ${!beatles[@]} //直接反向調用相當於依次列印了 singer bassist drummer guitarist 11 do 12     echo "the ${instrument} is ${beatles[$instrument]}" 13 done~


以上兩個程式都是關係數組的表達。

第一個引用,第二個反向引用${!array[@]},關係數組將值與索引關係到一起使用的時候必須先

 declare -A

來聲明


來練練手弄個簡單的,lee is chinsese people ,kobe is us people .dirk is german people

  1 #########################################################################      2 # File Name: dirk.sh     3 # Author: dty      4 # mail: [email protected]      5 # Created Time: Wed 05 Aug 2015 05:02:53 PM CST      6 #########################################################################      7 #!/bin/bash      8 declare -A people      9 people=( [chinese]=lee [us]=kobe [german]=dirk )     10 for ship in chinese us german     11 do     12     echo the ${people[$ship]} is  $ship people     13 done    [[email protected] mnt]# ./dirk.sh     the lee is chinese people     the kobe is us people     the dirk is german people

shell指令碼-關係數組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.