Array of arrays in bash

前端 未结 6 1126
日久生厌
日久生厌 2020-12-24 05:09

I\'m attempting to read an input file line by line which contains fields delimited by periods. I want to put them into an array of arrays so I can loop through them later on

6条回答
  •  -上瘾入骨i
    2020-12-24 05:41

    Field nest box in bash but it can not circumvent see the example.

    #!/bin/bash
    
    # requires bash 4 or later; on macOS, /bin/bash is version 3.x,
    # so need to install bash 4 or 5 using e.g. https://brew.sh
    
    declare -a pages
    
    pages[0]='domain.de;de;https'
    pages[1]='domain.fr;fr;http'
    
    for page in "${pages[@]}"
    do
        # turn e.g. 'domain.de;de;https' into
        # array ['domain.de', 'de', 'https']
        IFS=";" read -r -a arr <<< "${page}"
    
        site="${arr[0]}"
        lang="${arr[1]}"
        prot="${arr[2]}"
        echo "site : ${site}"
        echo "lang : ${lang}"
        echo "prot : ${prot}"
        echo
    done
    

提交回复
热议问题