【问题标题】:bash - unable to echo array valuebash - 无法回显数组值
【发布时间】:2022-10-13 18:36:53
【问题描述】:

我有四个摄像头,想将其中任何一个的输出存储在空数组中,并从任何数组成员获取输出代码。

        # 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]}

问题是echo ${outputcodes[0]} echo ${outputcodes} 什么也没显示

有什么问题 ?

当我将 echo streamresults 添加到 for 循环时,我得到了正确的结果

0
0
0
0

设置outputcodes+=($streamresults) 没有任何改变

【问题讨论】:

  • 请将declare -p outputcodes 的输出添加到您的问题中(此处不做评论)。

标签: arrays bash


【解决方案1】:

您没有正确附加到outputcodes。换行:

outputcodes+=$streamresults

outputcodes+=($streamresults)

您可以使用以下命令回显最终输出:

echo ${outputcodes[@]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 2017-12-14
    • 2015-03-02
    • 2017-04-30
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多