カテゴリ
HTML PostgreSQL linux

【シェルスクリプト】二次元連想配列の作り方

シェルスクリプトで二次元連想配列を表す方法


    #declareコマンドでオプションAを付け、arrayという連想配列を定義する
    declare -A array
    array=(
        ['test1']='test1_1 test1_2'
        ['test2']='test3'
    )
    echo ${array[test1]}
    #結果 test1_1 test1_2

    echo ${array[test2]}
    #結果 test3
    
    echo ${array[@]}
    #結果 test1_1 test1_2 test3

  

シェルスクリプトでは、配列の中身をスペース区切りで判断する。


    check=${array[test1]}
    echo ${check}
    #結果 test1_1 test1_2

check変数に配列を表す「()」を付けて代入することで配列として扱える。


    check=(${array[test1]})
    echo ${check}
    #結果 test1_1
    
    echo ${check[0]}
    #結果 test1_1
    
    echo ${check[1]}
    #結果 test1_2
    
    echo ${check[@]}
    #結果 test1_1 test1_2