Bash Associative Arrays - Bash 460

Bash Associative Arrays – Bash 460

  • Post author:
  • Post category:IT / Bash
  • Post comments:0 Comments
  • Post last modified:2023-02-13

Bash Associative Arrays

陣列用於存儲多個有索引的資料,每個陣列元素的值由該元素對應的索引值訪問,Associative Arrays Bash 可以將字串存儲為索引或鍵的陣列,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- arrays
       +- associative_arrays_test.sh

單元測試

Bash Associative Arrays 提供新增、取得、修改、刪除等操作陣列中的元素。

testAdd

建立一個 Array,增加三個元素。

testAdd () {
    local expectedLength=3
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Lemon Grape Kiwifruit
3 1 2

testAddByIndex

建立一個 Array,內有三個元素,指定位置增加第四個元素。

testAddByIndex () {
    local expectedLength=4
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    array[Mango]=4
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Lemon Mango Grape Kiwifruit
3 4 1 2

testGet

建立一個 Array,內有三個元素,取得指定位置元素。

testGet () {
    local expected=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${array[Kiwifruit]}
    assertEquals "get" $expected ${array[Kiwifruit]}
}
2

testDelete

建立一個 Array,內有三個元素,刪除指定位置元素。

testDelete () {
    local expectedLength=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    unset array[Grape]
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Lemon Kiwifruit
3 2

testDeleteAll

建立一個 Array,內有三個元素,刪除所有元素。

testDeleteAll () {
    local expectedLength=0
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    unset array
    echo ${!array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLength

建立一個 Array,內有三個元素,取得陣列長度。

testLength () {
    local expectedLength=3
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${#array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
3

testLoop

建立一個 Array,內有三個元素,迴圈取得元素。

testLoop () {
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    for i in "${!array[@]}"
    do
        echo $i
    done
}
Lemon
Grape
Kiwifruit

testSet

建立一個 Array,內有三個元素,修改指定位置元素。

testSet () {
    local expected=10
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${!array[@]}
    echo ${array[@]}
    array[Grape]=10
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "get" $expected ${array[Grape]}
}
#before
Lemon Grape Kiwifruit
3 1 2

#after
Lemon Grape Kiwifruit
3 10 2

testSlice

建立一個 Array,內有三個元素,取得指定索引元素。

testSlice () {
    local expectedLength=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    local sliceArray=("${array[@]:1:2}")
    echo ${sliceArray[@]}
    assertEquals "length" $expectedLength ${#sliceArray[@]}
}
3 1

associative_arrays_test.sh

Array in Bash 新增單元測試,驗證是否符合預期。

#!/bin/bash

testAdd () {
    local expectedLength=3
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testAddByIndex () {
    local expectedLength=4
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    array[Mango]=4
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testGet () {
    local expected=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${array[Kiwifruit]}
    assertEquals "get" $expected ${array[Kiwifruit]}
}

testDelete () {
    local expectedLength=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    unset array[Grape]
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testDeleteAll () {
    local expectedLength=0
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    unset array
    echo ${!array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLength () {
    local expectedLength=3
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${#array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLoop () {
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    for i in "${!array[@]}"
    do
        echo $i
    done
}

testSet () {
    local expected=10
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    echo ${!array[@]}
    echo ${array[@]}
    array[Grape]=10
    echo ${!array[@]}
    echo ${array[@]}
    assertEquals "get" $expected ${array[Grape]}
}

testSlice () {
    local expectedLength=2
    declare -A array
    array=([Grape]=1 [Kiwifruit]=2 [Lemon]=3)
    local sliceArray=("${array[@]:1:2}")
    echo ${sliceArray[@]}
    assertEquals "length" $expectedLength ${#sliceArray[@]}
}

# Load shunit2
. shunit2

心得分享

Associative Array in Bash 提供了 Array 操作範例, Arrays Bash 在應用上相當廣泛,熟悉 Bash Array 常見的操作方式,可以快速撰寫程式,降低錯誤率,再輔以單元測試驗證,建置高效穩定的服務或系統。

發佈留言