0

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 ?

Yehudi
  • 189
  • 1
  • 1
  • 11
  • the example now expresses not only the quit action but also other actions that depend on MY_PATTERN – Yehudi Oct 04 '13 at 00:53

1 Answers1

0

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.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Thank your reply, for me this solution has 2 problems. Firstly the MY_PATTERN may a regular expression that need following detection and select case in bash. Secondly the action is to stop ffmpeg by enter [q] in stdin – Yehudi Oct 03 '13 at 09:21
  • this solution the some_command would be executed when ffmpeg finish, but what I try to achieve is to stop ffmpeg – Yehudi Oct 03 '13 at 09:29
  • @Yehudi Did you try it? As mentioned above, this would send a `SIGPIPE` to `ffmpeg`. That should stop it as soon as the pattern is found. – devnull Oct 03 '13 at 10:18
  • Hi devnull, you are right, grep -q "[MY_PATTERN]" would stop the ffmpeg. But I hope to do some decision (one of the decision is to stop ffmpeg) according to the line included MY_PATTERN. Do you think it's possible to handle subprocess's stdin/stdout on bash? – Yehudi Oct 03 '13 at 11:05
  • 1
    @Yehudi It might help if you update the question with scenarios. – devnull Oct 03 '13 at 11:10
  • @Yehudi No, it isn't. Examples might help. – devnull Oct 03 '13 at 11:43
  • modify again, the example now express not only the `quit` action but also other actions that depend on MY_PATTERN. – Yehudi Oct 04 '13 at 00:48