8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

如何在 Bash 中声明二维数组

zarez 1月前

54 0

我想知道如何在 bash 中声明一个二维数组,然后将其初始化为 0。在 C 中它看起来像这样:int a[4][5] = {0}; 以及如何为元素赋值?如在 C 中:a[2][3] = 3;

我想知道如何在 bash 中声明一个二维数组然后将其初始化为 0。

在 C 语言中它看起来像这样:

int a[4][5] = {0};

那么如何为元素赋值呢?例如在 C 中:

a[2][3] = 3;
帖子版权声明 1、本帖标题:如何在 Bash 中声明二维数组
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由zarez在本站《bash》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 在 bash 中模拟数组的一种方法(可以适应任意维数的数组):

    #!/bin/bash
    
    ## The following functions implement vectors (arrays) operations in bash:
    ## Definition of a vector <v>:
    ##      v_0 - variable that stores the number of elements of the vector
    ##      v_1..v_n, where n=v_0 - variables that store the values of the vector elements
    
    VectorAddElementNext () {
    # Vector Add Element Next
    # Adds the string contained in variable $2 in the next element position (vector length + 1) in vector $1
    
        local elem_value
        local vector_length
        local elem_name
    
        eval elem_value=\"\$$2\"
        eval vector_length=\$$1\_0
        if [ -z "$vector_length" ]; then
            vector_length=$((0))
        fi
    
        vector_length=$(( vector_length + 1 ))
        elem_name=$1_$vector_length
    
        eval $elem_name=\"\$elem_value\"
        eval $1_0=$vector_length
    }
    
    VectorAddElementDVNext () {
    # Vector Add Element Direct Value Next
    # Adds the string $2 in the next element position (vector length + 1) in vector $1
    
        local elem_value
        local vector_length
        local elem_name
    
        eval elem_value="$2"
        eval vector_length=\$$1\_0
        if [ -z "$vector_length" ]; then
            vector_length=$((0))
        fi
    
        vector_length=$(( vector_length + 1 ))
        elem_name=$1_$vector_length
    
        eval $elem_name=\"\$elem_value\"
        eval $1_0=$vector_length
    }
    
    VectorAddElement () {
    # Vector Add Element
    # Adds the string contained in the variable $3 in the position contained in $2 (variable or direct value) in the vector $1
    
        local elem_value
        local elem_position
        local vector_length
        local elem_name
    
        eval elem_value=\"\$$3\"
        elem_position=$(($2))
        eval vector_length=\$$1\_0
        if [ -z "$vector_length" ]; then
            vector_length=$((0))
        fi
    
        if [ $elem_position -ge $vector_length ]; then
            vector_length=$elem_position
        fi
    
        elem_name=$1_$elem_position
    
        eval $elem_name=\"\$elem_value\"
        if [ ! $elem_position -eq 0 ]; then
            eval $1_0=$vector_length
        fi
    }
    
    VectorAddElementDV () {
    # Vector Add Element
    # Adds the string $3 in the position $2 (variable or direct value) in the vector $1
    
        local elem_value
        local elem_position
        local vector_length
        local elem_name
    
        eval elem_value="$3"
        elem_position=$(($2))
        eval vector_length=\$$1\_0
        if [ -z "$vector_length" ]; then
            vector_length=$((0))
        fi
    
        if [ $elem_position -ge $vector_length ]; then
            vector_length=$elem_position
        fi
    
        elem_name=$1_$elem_position
    
        eval $elem_name=\"\$elem_value\"
        if [ ! $elem_position -eq 0 ]; then
            eval $1_0=$vector_length
        fi
    }
    
    VectorPrint () {
    # Vector Print
    # Prints all the elements names and values of the vector $1 on separate lines
    
        local vector_length
    
        vector_length=$(($1_0))
        if [ "$vector_length" = "0" ]; then
            echo "Vector \"$1\" is empty!"
        else
            echo "Vector \"$1\":"
            for ((i=1; i<=$vector_length; i++)); do
                eval echo \"[$i]: \\\"\$$1\_$i\\\"\"
                ###OR: eval printf \'\%s\\\n\' \"[\$i]: \\\"\$$1\_$i\\\"\"
            done
        fi
    }
    
    VectorDestroy () {
    # Vector Destroy
    # Empties all the elements values of the vector $1
    
        local vector_length
    
        vector_length=$(($1_0))
        if [ ! "$vector_length" = "0" ]; then
            for ((i=1; i<=$vector_length; i++)); do
                unset $1_$i
            done
            unset $1_0
        fi
    }
    
    ##################
    ### MAIN START ###
    ##################
    
    ## Setting vector 'params' with all the parameters received by the script:
    for ((i=1; i<=$#; i++)); do
        eval param="\${$i}"
        VectorAddElementNext params param
    done
    
    # Printing the vector 'params':
    VectorPrint params
    
    read temp
    
    ## Setting vector 'params2' with the elements of the vector 'params' in reversed order:
    if [ -n "$params_0" ]; then
        for ((i=1; i<=$params_0; i++)); do
            count=$((params_0-i+1))
            VectorAddElement params2 count params_$i
        done
    fi
    
    # Printing the vector 'params2':
    VectorPrint params2
    
    read temp
    
    ## Getting the values of 'params2'`s elements and printing them:
    if [ -n "$params2_0" ]; then
        echo "Printing the elements of the vector 'params2':"
        for ((i=1; i<=$params2_0; i++)); do
            eval current_elem_value=\"\$params2\_$i\"
            echo "params2_$i=\"$current_elem_value\""
        done
    else
        echo "Vector 'params2' is empty!"
    fi
    
    read temp
    
    ## Creating a two dimensional array ('a'):
    for ((i=1; i<=10; i++)); do
        VectorAddElement a 0 i
        for ((j=1; j<=8; j++)); do
            value=$(( 8 * ( i - 1 ) + j ))
            VectorAddElementDV a_$i $j $value
        done
    done
    
    ## Manually printing the two dimensional array ('a'):
    echo "Printing the two-dimensional array 'a':"
    if [ -n "$a_0" ]; then
        for ((i=1; i<=$a_0; i++)); do
            eval current_vector_lenght=\$a\_$i\_0
            if [ -n "$current_vector_lenght" ]; then
                for ((j=1; j<=$current_vector_lenght; j++)); do
                    eval value=\"\$a\_$i\_$j\"
                    printf "$value "
                done
            fi
            printf "\n"
        done
    fi
    
    ################
    ### MAIN END ###
    ################
    
返回
作者最近主题: