Manipulating an array (printed by php-cli) in shell script

前端 未结 3 1616
情深已故
情深已故 2021-01-23 14:54

I am a newbie with shell scripts and I learnt a lot today. This is an extension to this question Assigning values printed by PHP CLI to shell variables

I got the solutio

3条回答
  •  太阳男子
    2021-01-23 15:51

    You don't say what shell you're using, but assuming it's one that supports arrays:

    result=($(getConfigVals))    # you need to create an array before you can ...
    
    for((cnt=0;cnt<${#result};cnt++))
    do
        echo ${result[$cnt]}" - "$cnt    # ... access it using a subscript
    done
    

    This is going to be an indexed array, rather than an associative array. While associative arrays are supported in Bash 4, you'll need to use a loop similar to the one in Martin Kosek's answer for assignment if you want to use them.

提交回复
热议问题