bash script, control processes with stdin/stdout

798 Views Asked by At

Please help to advise how to use bash to catch the sub-process's stdout and send character to sub-process's stdin.

For example, use bash to control 10 videos convert by ffmpeg process, bash code needs to watch each ffmpeg process's stdout then decides if send the stdin command [+]/[-]/[c]/[q] or others command to control ffmpeg

the covert job would be something like

ffmpeg -i INPUT_n -c copy -f flv out_n.flv 2>&1 | grep "[MY_PATTERN]"

only when [MY_PATTERN] occurs this job shows word on it's stdout. I would like to use bash code to catch the job's stdout, do some decision according to the line included MY_PATTERN then feed command into the job's stdin. I guess I need to active new shell by bash to execute the job and interact its stdin/stdout. But how to ?

1

There are 1 best solutions below

7
devnull On

If I inderstood the question correct, you need to use grep -q "MY_PATTERN". This would send a SIGPIPE to the ffmpeg process as soon as the pattern is matched.

The following should work for you:

ffmpeg -i INPUT_n -c copy -f flv out_n.flv 2>&1 | grep -q "[MY_PATTERN]" && some_command

some_command would be executed as soon as the pattern is matched.