Home > Enterprise >  bash - unable to echo array value
bash - unable to echo array value

Time:10-13

I have four cameras and want to store output from any of it in empty array and get output code from any array member.

        # streams to check
        streams=("rtsp://Streaming/Channels/01" "rtsp://Streaming/Channels/201" "rtsp://Streaming/Channels/301" "rtsp://Streaming/Channels/401")
        # declare array for stream codes
        declare -a  outputcodes
           
        for stream in "${streams[@]}"; do
           streamoutput=$(timeout 20s ffprobe -v quiet -print_format json -show_streams $stream)
           # get output code for each stream  
           streamresults=$(jq -r '.streams[0].index' <<< "$streamoutput")
          # add stream result to array          
          outputcodes =$streamresults
           done
        exit 0
    # get first array member result   
    echo ${outputcodes[0]}

the problem is that echo ${outputcodes[0]} echo ${outputcodes} shows nothing

What's the problem ?

when i add echo $streamresults to for loop i'm getting correct results

0
0
0
0

setting outputcodes =($streamresults) changes nothing

Bash version:4.2.46(2)-release

Tried to declare array in this way but same output:

declare -a outputcodes=()

CodePudding user response:

You are not appending to outputcodes correctly. Replace the line:

outputcodes =$streamresults

with

outputcodes =($streamresults)

You can echo the final output with:

echo ${outputcodes[@]}

CodePudding user response:

I'm an idiot, had exit 0 before echoing array, once removed it all works. Sorry for bothering

  • Related