在 bash 脚本中执行命令,直到输出超过特定值

execute command in bash script until output exceeds certain value

我使用一个命令来解析特定帧的视频文件并在找到时返回它们的时间码。目前,我必须执行命令,等待,直到打印到 stdout 的值达到所需位置,然后使用 Ctrl+C[=69 中止执行=].

因为我必须观察过程并在适当的时候中止执行以获得我需要的信息,所以我想,我可以通过创建一个 bash 脚本在某种程度上自动化它。

我不确定,如果它可以在 bash 中完成,因为我不完全知道,如何中止与它写入标准输出的值有关的执行。

命令的输出看起来像

0.040000
5.040000
10.040000
15.040000
18.060000
(...)

我试过了

until [[ "$timecode" -gt 30 ]]; do
  timecode=$(mycommand)
  sleep 0.1
done

echo "Result: $timecode"

while [[ "$timecode" -le 30 ]]; do
  timecode=$(mycommand)
  sleep 0.1
done

echo "Result: $timecode"

这两者似乎都导致命令一直执行到它完成,然后循环的其余部分正在处理。但我想在命令执行时评估输出并根据输出中断执行。

附加信息

该命令无法在流中的特定点停止。它解析整个文件并给出结果,除非发出停止信号。这是我的第一枪。

命令的执行时间很长,因为我解析的文件是~2GB。因为我不需要文件的所有帧,而只需要给定时间码周围的几帧,所以我从不让它执行直到完成。

命令的输出因文件而异,因此我无法查找确切的值。如果我知道确切的值,我可能就不必寻找它了。

目标时间代码 - 在示例中由“-gt 30”指定 - 对于我必须解析的每个文件都是不同的,因此一旦脚本运行,我必须将其放入命令行参数.我还必须确保返回的不仅仅是执行的最后一个值,而是大约最后 5 个值。这两个我已经有想法了。

我完全被那个问题困住了,甚至不知道要 google 干什么。

感谢您的意见!

曼纽尔


根据 PSkocik 和 Kyle Burton 的回答,我能够将建议的解决方案整合到我的脚本中。它不起作用,我不明白为什么。

这里是完整的脚本,包括提供输出的外部命令:

 #!/usr/bin/env bash
 set -eu -o pipefail

 parser () {
   local max=""
   local max_int

   max_int="${max%.*}"

   while read tc;
     do
       local tc_int
       tc_int="${tc%.*}"
       echo $tc

       if (( "$tc_int" >= "$max_int" )); then
         echo "Over 30: $tc";
         exec 0>&-
         return 0
       fi

     done
 }

 ffprobe "" -hide_banner -select_streams v -show_entries frame=key_frame,best_effort_timestamp_time -of csv=nk=1:p=0:s="|" -v quiet | sed -ne "s/^1|//p" | parser 30

我没有从 "echo $tc" 得到任何输出,但 ffprobe 是 运行 - 我可以在顶部看到它。它一直运行到我使用 Ctrl+C.

停止脚本

感谢凯尔为此付出的巨大努力。我永远不会得出这样的结论。我根据您的建议更改了 ffprobe 的命令行

 ffprobe "" -hide_banner -select_streams v -show_entries frame=key_frame,best_effort_timestamp_time -of csv=nk=1:p=0:s="|" -v quiet | cut -f2 -d\| | parser 30

现在,我在 ffprobe 运行时得到了结果。但是...您更改命令 returns 所有帧的方式,ffprobe 发现而不仅仅是关键帧。 ffprobe 命令的原始输出看起来像

 1|0.000000
 0|0.040000
 0|0.080000
 0|0.120000
 0|0.160000
 0|0.200000
 (...)

行首的0表示:这不是关键帧。 行首的1表示:这是一个关键帧。

该脚本旨在仅提供视频文件特定时间码周围的关键帧。您更改命令的方式,它现在提供视频文件的所有帧,这使得结果输出无用。必须过滤掉所有以零开头的行。

由于我不太明白,为什么这不适用于sed,我只能尝试通过尝试和错误找到解决方案,以方便不同的工具来过滤输出。但如果过滤本身导致问题,我们可能会在这里碰壁。

如果您有进程 a 将内容输出到标准输出,进程 b 通过管道读取输出的内容:

a | b

所有 b 通常必须在输出某个项目时杀死 a 就是关闭它的标准输入。

样本b:

b()
{
    while read w;
        do case $w in some_pattern)exec 0>&-;; esac; 
        echo $w
    done
}

关闭标准输入(文件描述符 0)将导致生产者进程在尝试进行下一次写入时被 SIGPIPE 杀死。

我认为 PSkocik 的做法是有道理的。我认为您需要做的就是 运行 您的 mycommand 并将其通过管道传输到您的 while 循环中。如果您将 PSkocik 的代码放入文件 wait-for-max.sh 中,那么您应该能够 运行 将其作为:

mycommand | bash wait-for-max.sh

在上面的评论中与 M. Uster 合作后,我们提出了以下解决方案:

#!/usr/bin/env bash
set -eu -o pipefail

# echo "bash cutter.sh rn33.mp4"

# From: 
# test -f stack_overflow_q45304233.tar ||  curl -k -O https://84.19.186.119/stack_overflow_q45304233.tar
# test -f stack_overflow_q45304233.tar ||  curl -k -O https://84.19.186.119/stack_overflow_q45304233.tar
# test -f rn33.mp4 || curl -k -O https://84.19.186.119/rn33.mp4

function parser () {
  local max=""
  local max_int

  # NB: this removes everything after the decimal point
  max_int="${max%.*}"

  # I added a line number so I could match up the ouptut from this function
  # with the output captured by the 'tee' command
  local lnum="0"
  while read -r tc;
    do

      lnum="$(( 1 + lnum ))"

      # if a blank line is read, just ignore it and continue
     if [ -z "$tc" ]; then
       continue
     fi

     local tc_int
     # NB: this removes everything after the decimal point
     tc_int="${tc%.*}"
     echo "Read[$lnum]: $tc"

     if (( "$tc_int" >= "$max_int" )); then
       echo "Over 30: $tc";
       # This closes stdin on this process, which will cause an EOF on the
       # process writing to us across the pipe
       exec 0>&-
       return 0
     fi

    done
}

# echo "bash version:    $BASH_VERSION"
# echo "ffprobe version: $(ffprobe -version | head -n1)"
# echo "sed version:     $(sed --version | head -n1)"

# NB: by adding in the 'tee ffprobe.out' into the pipeline I was able to see
# that it was producing lines like:
#
# 0|28.520000
# 1|28.560000
#
#
# changing the sed to look for any single digit and a pipe fixed the script
# another option is to use cut, see below, which is probalby more robust.

# ffprobe "" \
#   -hide_banner \
#   -select_streams v \
#   -show_entries frame=key_frame,best_effort_timestamp_time \
#   -of csv=nk=1:p=0:s="|" \
#   -v quiet 2>&1 | \
#   tee ffprobe.out |
#   sed -ne "s/^[0-9]|//p" | \
#   parser 30


ffprobe "" \
    -hide_banner \
    -select_streams v \
    -show_entries frame=key_frame,best_effort_timestamp_time \
    -of csv=nk=1:p=0:s="|" \
    -v quiet 2>&1 | \
    cut -f2 -d\| | \
    parser 30

在 PSkocik 的帮助和 Kyle Burton 的大力支持下,我的问题终于找到了答案。谢谢你们!

我不知道,可以将脚本中执行的命令的输出通过管道传输到属于该脚本的函数。这是第一个必要的信息。

而且我不知道,如何正确评估函数内部的管道信息以及如何从函数内部发出信号,即应该终止生成值的命令的执行。

此外,Kyle 发现,我通过将原始输出传输到 sed 并将生成的数据传输到脚本内的函数来进行的过滤禁止脚本按设计运行。我仍然不确定,为什么 - 但它确实如此。

生成输出的原始命令现在通过管道传输到脚本的内部函数。过滤是在函数内部完成的,以避免 sed 出现问题。现在一切正常,我可以继续完成脚本了。

这是灵魂的工作代码:

 #!/usr/bin/env bash
 set -eu -o pipefail

 function parser () {
   local max=""
   local max_int

   max_int="${max%.*}"

   while read tc;
     do

      #If line is empty, continue
      if [ -z "$tc" ]; then
        continue
      fi

      #If first char is 0 (=non-Index Frame), continue
      local iskey="${tc:0:1}";

      if [ $iskey == "0" ]; then
        continue
      fi

      #Return timecode if intended maximum has been reached
      local val="${tc:2:10}"
      local tc_int
      tc_int="${val%.*}"

      if (( "$tc_int" >= "$max_int" )); then
        echo "First index frame at/after given Timecode: $tc";
        exec 0>&-
        return 0
      fi

     done
 }

 ffprobe "" -hide_banner -select_streams v -show_entries frame=key_frame,best_effort_timestamp_time -of csv=nk=1:p=0:s="|" -v quiet | parser ""

用法:

 ./script.sh "Name of Movie.avi" 30

其中 30 表示搜索并返回下一个找到的索引帧的时间码。