Bash Arrays - Bash 460

Bash Arrays – Bash 460

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

Bash Arrays

陣列可以存放多個值,只支援一維陣列,並不支援多維陣列,Array Bash 初始化時不需要定義陣列大小,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- arrays
       +- arrays_test.sh

單元測試

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

testAdd

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

testAdd () {
    local expectedLength=3
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Durian Guava Pitaya

testAddByIndex

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

testAddByIndex () {
    local expectedLength=4
    local array=("Durian" "Guava" "Pitaya")
    #array+=("Grape")
    array[3]="Grape"
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Durian Guava Pitaya Grape

testGet

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

testGet () {
    local expected="Guava"
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[1]}
    assertEquals "get" $expected ${array[1]}
}
Guava

testDelete

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

testDelete () {
    local expectedLength=2
    local array=("Durian" "Guava" "Pitaya")
    unset array[0]
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
Guava Pitaya

testDeleteAll

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

testDeleteAll () {
    local expectedLength=0
    local array=("Durian" "Guava" "Pitaya")
    unset array
    echo ${!array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLength

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

testLength () {
    local expectedLength=3
    local array=("Durian" "Guava" "Pitaya")
    echo ${#array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}
3

testLoop

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

testLoop () {
    local array=("Durian" "Guava" "Pitaya")
    for i in "${array[@]}"
    do
        echo $i
    done
}
Durian
Guava
Pitaya

testSet

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

testSet () {
    local expected="Grape"
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[@]}
    array[0]="Grape"
    echo ${array[@]}
    assertEquals "get" $expected ${array[0]}
}
Durian Guava Pitaya
Grape Guava Pitaya

testSlice

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

testSlice () {
    local expectedLength=2
    local array=("Durian" "Guava" "Pitaya")
    local sliceArray=("${array[@]:1:2}")
    echo ${sliceArray[@]}
    assertEquals "length" $expectedLength ${#sliceArray[@]}
}
Guava Pitaya

arrays_test.sh

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

#!/bin/bash

testAdd () {
    local expectedLength=3
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testAddByIndex () {
    local expectedLength=4
    local array=("Durian" "Guava" "Pitaya")
    #array+=("Grape")
    array[3]="Grape"
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testGet () {
    local expected="Guava"
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[1]}
    assertEquals "get" $expected ${array[1]}
}

testDelete () {
    local expectedLength=2
    local array=("Durian" "Guava" "Pitaya")
    unset array[0]
    echo ${array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testDeleteAll () {
    local expectedLength=0
    local array=("Durian" "Guava" "Pitaya")
    unset array
    echo ${!array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLength () {
    local expectedLength=3
    local array=("Durian" "Guava" "Pitaya")
    echo ${#array[@]}
    assertEquals "length" $expectedLength ${#array[@]}
}

testLoop () {
    local array=("Durian" "Guava" "Pitaya")
    for i in "${array[@]}"
    do
        echo $i
    done
}

testSet () {
    local expected="Grape"
    local array=("Durian" "Guava" "Pitaya")
    echo ${array[@]}
    array[0]="Grape"
    echo ${array[@]}
    assertEquals "get" $expected ${array[0]}
}

testSlice () {
    local expectedLength=2
    local array=("Durian" "Guava" "Pitaya")
    local sliceArray=("${array[@]:1:2}")
    echo ${sliceArray[@]}
    assertEquals "length" $expectedLength ${#sliceArray[@]}
}


# Load shunit2
. shunit2

心得分享

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

發佈留言