为什么此Shell命令不起作用? (“命令”有效,但“命令|猫”无效)

时间:2018-10-16 21:35:20

标签: linux bash shell ffmpeg grep

问题

以下代码输出类似...

$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep -A 2 key_frame=1
key_frame=1
pkt_pts=0
pkt_pts_time=0:00:00.000000
--
key_frame=1
pkt_pts=41041
pkt_pts_time=0:00:01.710042
--
key_frame=1
pkt_pts=64064
pkt_pts_time=0:00:02.669333
--
key_frame=1
pkt_pts=87087
pkt_pts_time=0:00:03.628625
--
...

但是下面的内容什么也没输出。

$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep -A 2 key_frame=1|cat

然后...

$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep key_frame=1
key_frame=1
key_frame=1
key_frame=1
key_frame=1
$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep key_frame=1|cat
# Nothing outputted.

为什么?

预期结果

我真正想做的是

$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep -A 2key_frame=1|grep time
pkt_pts_time=0:00:00.000000
pkt_pts_time=0:00:01.710042
pkt_pts_time=0:00:02.669333
pkt_pts_time=0:00:03.628625
...

但是结果是

$ ffprobe -show_frames -select_streams v test.mp4 2>/dev/null|grep -A 2key_frame=1|grep time
# Nothing outputted.

Grep的工作原理是在ffprobe之后。

$ seq 30|grep 1|grep 2
12
21

环境

  • Windows 10 Pro上Ubuntu上的Bash
    • Windows版本:1803
    • Windows操作系统内部版本:17134.345

Ubuntu的环境:

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"

$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ ffprobe
ffprobe version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2007-2018 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Simple multimedia streams analyzer
usage: ffprobe [OPTIONS] [INPUT_FILE]

You have to specify one input file.
Use -h to get full help or, even better, run 'man ffprobe'.

出什么问题了?

grep或ffprobe是否通过管道in this case的存在来改变其行为? 谢谢。

1 个答案:

答案 0 :(得分:1)

这是重现问题的简便方法:

$ while sleep 1; do date; done | grep ""
Tue Oct 16 14:51:25 PDT 2018
Tue Oct 16 14:51:26 PDT 2018
[...]

$ while sleep 1; do date; done | grep "" | cat
(no output)

之所以会发生这种情况,是因为当stdout是tty时,libc将默认为行缓冲输出,否则默认为完全缓冲输出(例如使用cat的管道)。如果您等待,它仍然会产生正确的输出-只会一次批量进行。

在这种情况下,这是由于grep造成的,因此您可以使用--line-buffered

$ while sleep 1; do date; done | grep --line-buffered "" | cat
Tue Oct 16 14:55:53 PDT 2018
Tue Oct 16 14:55:54 PDT 2018

许多其他命令都有明确地执行此操作的方法。对于那些不支持的方法,请参见Unix&Linux SE上的Turn off buffering in pipe,以了解各种欺骗方法。