David Shaw
David Shaw

Reputation: 27

Bash - control external command's output

I'm writing a bash script to make DVD authoring more automated (but, mainly, so that I can learn some more bash scripting) and I'm trying to find out if it's possible to control how an exterrnal command presents its output.

For instance, the output from ffmpeg is a load of (to me) irrelevant cruft about options, libraries, streams, progress and so on.

What I really want is to be able to select for display only the lines with the input and output filenames and then to display the progress on the same line each time. Similarly for mkisofs and wodim.

I've tried Googling for this and am beginning to suspect that either it's not possible or nobody's thought of it before (or, possibly, that it's so obvious that nobody thinks it necessary to say how :-) ).

Many thanks, in advance,

David Shaw

Upvotes: 0

Views: 167

Answers (1)

dboals
dboals

Reputation: 610

You want to use grep and pipes. They are your friends. You want to pipe the output of the ffmpeg into grep and have it output only lines containing the text you want.

Assuming you have the input and output file names as command lines arguments $1 and $2 to your shell script, you might try something like

ffmpeg .... | grep "$1\|$2"
            ^          ^
            |          +--  escape and OR character
            +--pipe character

The '\|' is an escape and an OR character for regular expressions. The OR '|' is also the pipe character so you have to escape that.

This will output only output lines that contain the files you are looking for.

This assumes all output is via stdout. If ffmpeg is outputting text via stderr then you will need to add some redirects at the end of ffmpeg line to redirect those back to stdout.

EDIT: I used the wrong quotes in the first example. Use double quotes or it won't expand the parameters $1 and $2

Upvotes: 2

Related Questions