使用while、for和if进行Bash,直到添加第二个值

使用while、for和if进行Bash,直到添加第二个值,bash,if-statement,for-loop,while-loop,jq,Bash,If Statement,For Loop,While Loop,Jq,我有一个脚本,使用jq从curl语句中提取值。每当if语句接收到一个真值时,该脚本都可以正常工作,但是,一旦找到第二个媒体包,它就会中断,并且必须进行处理 守则: #!/bin/bash source mh_auth.sh rm -f times.txt touch times.txt #Read lines from file with mediapackage-id's that are on schedule. while read LINE; do curl -

我有一个脚本,使用jq从curl语句中提取值。每当if语句接收到一个真值时,该脚本都可以正常工作,但是,一旦找到第二个媒体包,它就会中断,并且必须进行处理

守则:

#!/bin/bash

source mh_auth.sh

rm -f times.txt
touch times.txt

#Read lines from file with mediapackage-id's that are on schedule.
while read LINE; do


        curl --silent --digest -u $mh_username:$mh_password -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" "$mh_server/workflow/instances.json?mp=$LINE" >  $LINE-curl.txt

        #Format the file to make it more readable if you need to troubleshoot
        /usr/bin/python -m json.tool $LINE-curl.txt > $LINE-curl-final.txt

        #Test to see if this mediapackage has been published yet, and if it has, extract the necessary values for calculation

        if grep -q -e 'Cleaning up"' $LINE-curl-final.txt; then

                echo "Media Package found"
                workflows=$( jq '.workflows.workflow[]' < $LINE-curl-final.txt )

                for i in "${workflows}"
                do
                        echo "Getting the end_time variable"
                        end_time=`echo ${i} | jq '.operations.operation[] | select(.description == "Cleaning up") | .completed'`
                        echo "Done getting end time variable"
                        echo "Getting ingest time variable"
                        ingest_time=`echo ${i} | jq '.operations.operation[] | select(.description == "Ingest") | .completed'`
                        echo "Done getting ingest time variable"

                        echo $ingest_time  $end_time  >> times.txt
                        echo last >> times.txt

                done

        else
                echo "Media Package not published yet"
        fi

        rm -f $LINE-curl.txt
        rm -f $LINE-curl-final.txt

done < scheduled-mediapackages.txt
每当我将第二个发布的媒体包(包含与第一个完全相同的json)添加到我的列表中时,我会得到以下信息:

Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package not published yet
Media Package found
Getting the end_time variable
Done getting end time variable
Getting ingest time variable
Done getting ingest time variable
Media Package not published yet
Media Package not published yet
Media Package found
Getting the end_time variable
Done getting end time variable
Getting ingest time variable
Done getting ingest time variable
Media Package found
Getting the end_time variable
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
Done getting end time variable
Getting ingest time variable
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot index string with string
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
jq: error: Cannot iterate over null
Done getting ingest time variable

有没有办法解决这个问题?我一直在兜圈子,无法让它工作?

我知道回答我自己的问题似乎有些傲慢,但对于所有那些像我一样挣扎的人,我想补充一个解决方案。在我得到任何关于代码的抨击之前,可能是笨拙和“太多了”,请记住,我刚刚开始用bash编写代码,这是可行的。其他人可能会发现这一点,并凭借他们的经验将其清理干净。但对我来说,这是有效的,所以我坚持下去

多亏了马克·塞切尔,我又回到了画板上,想弄明白为什么while循环给我带来了困难。我发现我得到的一些json返回中的对象比其他的多,因此必须调整jq命令以适应每个命令。我只是发布了提取我在OP中寻找的数据的相关代码

if grep -q -e 'Cleaning up"' $LINE-curl-final.txt; then
由于某些媒体包在第一次处理时失败,然后被“拾取”以重新处理,因此它们有多个工作流对象。这个if语句在它们之间搜索成功的一个,并使用调整后的jq查询提取数据

get_count=$( jq -r '.[] | .totalCount' < $LINE-curl-final.txt )                
    if  [[ "$get_count" -gt 1 ]]; then

       state=$( jq -r '.workflows.workflow[] | select(.state == "SUCCEEDED" ) | .operations.operation[] | select(.description == "Cleaning up") | .state' < $LINE-curl-final.txt )

       if [ "$state" = "SUCCEEDED" ];then

           end_time=$( jq '.workflows.workflow[] | select(.state == "SUCCEEDED" ) | .operations.operation[] | select(.description == "Cleaning up") | .completed' < $LINE-curl-final.txt )
           ingest_time=$( jq '.workflows.workflow [] | .operations.operation[] | select(.description == "Ingest") | .completed' < $LINE-curl-final.txt )

       fi

   else
       state=$(  jq -r '.workflows.workflow.operations.operation[] | select(.description == "Cleaning up") | .state' < $LINE-curl-final.txt )
       if [ "$state" = "SUCCEEDED" ];then

           end_time=$( jq '.workflows.workflow.operations.operation[] | select(.description == "Cleaning up") | .completed' < $LINE-curl-final.txt )
           ingest_time=$( jq '.workflows.workflow.operations.operation[] | select(.description == "Ingest") | .completed' < $LINE-curl-final.txt )

       fi

    fi


我希望这个想法可能会帮助其他人解决同样的问题。

尝试删除$workflows中的
for I的双引号
,或者
workflows
是一个数组?在这种情况下,您可能需要这样创建它:
IFS=$'\n'workflows=($(jq…)
@MarkSetchell谢谢您的回复!如果使用第二条评论的声明,我会得到以下信息:
媒体包尚未发布,找到媒体包获取结束时间变量解析错误:未完成的JSON术语完成获取结束时间变量获取摄取时间变量解析错误:未完成的JSON术语完成获取摄取时间变量媒体包找到获取结束时间变量jq:错误:无法为字符串编制索引,字符串已完成获取结束时间变量获取摄取时间变量jq:错误:无法为字符串编制索引,字符串已完成获取摄取时间变量
有什么想法吗?我所说的是“$workflows”中的I的
很可能是错误的,因为双引号将$workflows'的内容放在一个实体中,所以迭代不是一件好事,因为只有一个实体。@BashNewbie,您应该从代码示例中删除所有无关的内容。拿出一个简单的例子来说明这个问题。和
jq '.workflows.workflow.operations.operation[] | select(.description == "Cleaning up") | .completed'
jq '.workflows.workflow[] | select(.state == "SUCCEEDED" ) | .operations.operation[] | select(.description == "Cleaning up") | .completed'