2

我有一个脚本,我使用 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

成功运行会产生以下结果:

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

每当我将包含与第一个完全相同的 json 的第二个已发布媒体包添加到我的列表中时,我都会得到以下信息:

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

任何想法如何解决这一问题?我一直在兜圈子,无法让它工作?

4

1 回答 1

0

我知道回答我自己的问题可能看起来很自大,但是对于所有像我一样苦苦挣扎的人,我想添加一个修复程序。在我因为代码可能笨拙和“太多”而受到任何抨击之前,请记住,我刚刚开始使用 bash 进行编码,这是有效的。其他人可能会发现这一点,并凭借他们的经验能够清理它。不过对我来说,这是行得通的,所以我坚持了下来。

感谢 Mark Setchell,我回到了绘图板上,想弄清楚为什么 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

注意之间的区别

jq '.workflows.workflow.operations.operation[] | select(.description == "Cleaning up") | .completed'

jq '.workflows.workflow[] | select(.state == "SUCCEEDED" ) | .operations.operation[] | select(.description == "Cleaning up") | .completed'

我希望这个想法可以帮助其他在同样问题上苦苦挣扎的人。

于 2014-09-17T07:18:01.457 回答